我有一个目标数组[“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中实现?
当前回答
你可以这样做
let filteredArray = array.filter((elm) => {
for (let i=0; i<anotherAray.length; i++) {
return elm.includes(anotherArray[i])
}
})
其他回答
我写了3个解决方案。本质上,他们也是这样做的。他们一变为真就变为真。我写了三个解决方案,只是为了展示三种不同的做事方式。现在,这取决于你更喜欢什么。您可以使用performance.now()检查一个或另一个解决方案的性能。在我的解决方案中,我还检查了哪个阵列最大,哪个阵列最小,以提高操作效率。
第三种解决方案可能不是最可爱的,但很有效。我决定添加它,因为在一些编码面试中,您不允许使用内置方法。
最后,当然。。。我们可以用2个NESTED for循环(蛮力方法)提出解决方案,但您希望避免这种情况,因为时间复杂度是坏的O(n^2)。
注:
您可以使用.indexOf()。如果您检查该值是否大于0。如果值不存在会给你-1。如果它确实存在,它会给你大于0。
indexOf()与includes()
哪个性能更好?indexOf()稍有不同,但我认为includes更可读。
如果我没弄错的话,.includes()和indexOf()在幕后使用循环,所以当它们与.some()一起使用时,您将处于O(n^2)。
USING循环
const compareArraysWithIncludes = (arr1, arr2) => {
const [smallArray, bigArray] =
arr1.length < arr2.length ? [arr1, arr2] : [arr2, arr1];
for (let i = 0; i < smallArray.length; i++) {
return bigArray.includes(smallArray[i]);
}
return false;
};
使用.some()
const compareArraysWithSome = (arr1, arr2) => {
const [smallArray, bigArray] =
arr1.length < arr2.length ? [arr1, arr2] : [arr2, arr1];
return smallArray.some(c => bigArray.includes(c));
};
使用MAPS时间复杂度O(2n)=>O(n)
const compararArraysUsingObjs = (arr1, arr2) => {
const map = {};
const [smallArray, bigArray] =
arr1.length < arr2.length ? [arr1, arr2] : [arr2, arr1];
for (let i = 0; i < smallArray.length; i++) {
if (!map[smallArray[i]]) {
map[smallArray[i]] = true;
}
}
for (let i = 0; i < bigArray.length; i++) {
if (map[bigArray[i]]) {
return true;
}
}
return false;
};
我的代码:堆垛机
我既不是表演专家,也不是BigO,所以如果我说的有错,请告诉我。
我在node中使用下划线js提出了一个解决方案,如下所示:
var checkRole = _.intersection(['A','B'], ['A','B','C']);
if(!_.isEmpty(checkRole)) {
next();
}
ES6(最快)
const a = ['a', 'b', 'c'];
const b = ['c', 'a', 'd'];
a.some(v=> b.indexOf(v) !== -1)
2016年
const a = ['a', 'b', 'c'];
const b = ['c', 'a', 'd'];
a.some(v => b.includes(v));
强调
const a = ['a', 'b', 'c'];
const b = ['c', 'a', 'd'];
_.intersection(a, b)
演示:https://jsfiddle.net/r257wuv5/
jsPerf(性能):https://jsperf.com/array-contains-any-element-of-another-array
如果不需要类型强制(因为使用了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/
这种解决方案的缺点是,只能(正确地)使用数字和字符串(以及布尔值),因为这些值(隐式地)转换为字符串并设置为查找映射的键。对于非文字值,这不是很好/可能/容易做到的。
不确定这在性能方面可能有多高效,但这就是我使用数组解构来保持一切美好和简短的原因:
const shareElements = (arr1, arr2) => {
const typeArr = [...arr1, ...arr2]
const typeSet = new Set(typeArr)
return typeArr.length > typeSet.size
}
由于集合不能有重复的元素,而数组可以,因此组合两个输入数组,将其转换为集合,并比较集合大小和数组长度将告诉您它们是否共享任何元素。