我有一个目标数组[“apple”、“banana”、“orange”],我想检查其他数组是否包含任何一个目标阵列元素。
例如:
["apple","grape"] //returns true;
["apple","banana","pineapple"] //returns true;
["grape", "pineapple"] //returns false;
如何在JavaScript中实现?
我有一个目标数组[“apple”、“banana”、“orange”],我想检查其他数组是否包含任何一个目标阵列元素。
例如:
["apple","grape"] //returns true;
["apple","banana","pineapple"] //returns true;
["grape", "pineapple"] //returns false;
如何在JavaScript中实现?
当前回答
var target = ["apple","banana","orange"];
var checkArray = ["apple","banana","pineapple"];
var containsOneCommonItem = target.some(x => checkArray.some(y => y === x));`
["apple","grape"] //returns true;
["apple","banana","pineapple"] //returns true;
["grape", "pineapple"] //returns false;
其他回答
如果不需要类型强制(因为使用了indexOf),可以尝试以下方法:
var arr = [1, 2, 3];
var check = [3, 4];
var found = false;
for (var i = 0; i < check.length; i++) {
if (arr.indexOf(check[i]) > -1) {
found = true;
break;
}
}
console.log(found);
其中arr包含目标项。最后,find将显示第二个数组是否与目标至少匹配一次。
当然,您可以将数字换成任何您想要使用的东西-字符串很好,就像您的示例一样。
在我的具体示例中,结果应该为真,因为目标中存在第二个数组的3。
更新:
以下是我如何将其组织成一个函数(与之前的一些小变化):
var anyMatchInArray = (function () {
"use strict";
var targetArray, func;
targetArray = ["apple", "banana", "orange"];
func = function (checkerArray) {
var found = false;
for (var i = 0, j = checkerArray.length; !found && i < j; i++) {
if (targetArray.indexOf(checkerArray[i]) > -1) {
found = true;
}
}
return found;
};
return func;
}());
演示:http://jsfiddle.net/u8Bzt/
在这种情况下,可以修改函数,使targetArray作为参数传入,而不是在闭包中进行硬编码。
更新2:
虽然我上面的解决方案可能有效,并且(希望更)可读,但我认为处理我描述的概念的“更好”方法是做一些稍微不同的事情。上述解决方案的“问题”是,循环中的indexOf会导致目标数组对另一个数组中的每个项进行完全循环。这可以通过使用“查找”(一个映射…一个JavaScript对象文本)轻松“修复”。这允许在每个数组上进行两个简单的循环。下面是一个示例:
var anyMatchInArray = function (target, toMatch) {
"use strict";
var found, targetMap, i, j, cur;
found = false;
targetMap = {};
// Put all values in the `target` array into a map, where
// the keys are the values from the array
for (i = 0, j = target.length; i < j; i++) {
cur = target[i];
targetMap[cur] = true;
}
// Loop over all items in the `toMatch` array and see if any of
// their values are in the map from before
for (i = 0, j = toMatch.length; !found && (i < j); i++) {
cur = toMatch[i];
found = !!targetMap[cur];
// If found, `targetMap[cur]` will return true, otherwise it
// will return `undefined`...that's what the `!!` is for
}
return found;
};
演示:http://jsfiddle.net/5Lv9v/
这种解决方案的缺点是,只能(正确地)使用数字和字符串(以及布尔值),因为这些值(隐式地)转换为字符串并设置为查找映射的键。对于非文字值,这不是很好/可能/容易做到的。
嵌套调用.find()的Array.filter()将返回第一个数组中属于第二个数组的所有元素。检查返回数组的长度,以确定第一个数组中是否有第二个数组。
getCommonItems(firstArray, secondArray) {
return firstArray.filter((firstArrayItem) => {
return secondArray.find((secondArrayItem) => {
return firstArrayItem === secondArrayItem;
});
});
}
您正在寻找两个数组之间的交集。你有两种主要的交叉点类型:“每个”和“一些”。让我举几个好例子:
每
let brands1 = ['Ford', 'Kia', 'VW', 'Audi'];
let brands2 = ['Audi', 'Kia'];
// Find 'every' brand intersection.
// Meaning all elements inside 'brands2' must be present in 'brands1':
let intersectionEvery = brands2.every( brand => brands1.includes(brand) );
if (intersectionEvery) {
const differenceList = brands1.filter(brand => !brands2.includes(brand));
console.log('difference list:', differenceList);
const commonList = brands1.filter(brand => brands2.includes(brand));
console.log('common list:', commonList);
}
如果条件不满足(比如你在品牌中加入了“梅赛德斯”),那么“intersectionEvery”就不满足了——这将是错误的。
如果满足条件,它将把[“福特”、“大众”]列为区别,把[“起亚”、“奥迪”]列为了常见列表。
沙盒:https://jsfiddle.net/bqmg14t6/
SOME
let brands1 = ['Ford', 'Kia', 'VW', 'Audi'];
let brands2 = ['Audi', 'Kia', 'Mercedes', 'Land Rover'];
// Find 'some' brand intersection.
// Meaning some elements inside 'brands2' must be also present in 'brands1':
let intersectionSome = brands2.some( brand => brands1.includes(brand) );
if (intersectionSome) {
const differenceList = brands1.filter(brand => !brands2.includes(brand));
console.log('difference list:', differenceList);
const commonList = brands1.filter(brand => brands2.includes(brand));
console.log('common list:', commonList);
}
我们在这里寻找一些常见的品牌,但不一定全部。
它将把[“福特”、“大众”]列为不同品牌,把[“起亚”、“奥迪”]列为了共同品牌。
沙盒:https://jsfiddle.net/zkq9j3Lh/
香草JS
2016年:
const found = arr1.some(r=> arr2.includes(r))
ES6:
const found = arr1.some(r=> arr2.indexOf(r) >= 0)
它的工作原理
一些(..)根据测试函数检查数组的每个元素,如果数组的任何元素通过测试函数,则返回true,否则返回false。如果数组中存在给定参数,indexOf(..)>=0和includes(..)都返回true。
你可以这样做
let filteredArray = array.filter((elm) => {
for (let i=0; i<anotherAray.length; i++) {
return elm.includes(anotherArray[i])
}
})