我想比较一个数组和一个数组集。
例如,
array 1 = [[1,2,3],[1,4,5]];
array 2 = [1,3,6,5,4];
由于数组2中的元素1,4,5与数组1中的集合匹配,因此应返回true。
最佳答案
您可以遍历array1
并在每个数字组上使用every()
,并作为条件之一,将includes()
与第二个数组一起使用,语法相对较短:
var array1 = [
[1, 2, 3],
[1, 4, 5]
];
var array2 = [1, 3, 6, 5, 4];
var results = [];
array1.forEach(function(item, index, array) {
if (item.every(x => array2.includes(x))) {
results.push(item)
}
});
console.log(results)
编辑:我正在将返回true的结果推送到一个空数组...
关于javascript - 将数组与JavaScript中的数组进行比较,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/44510787/