我怎样才能INTERSECT
Javascript 中的 N 个数组在相当快的时间内完成?
即。
arr1 = [1,2,3,4,5];
arr2 = [1,2,3,4];
arr3 = [4];
arr4 = [4,5];
结果: [4]
最佳答案
您可以对具有 Set
的常见元素采用交集函数。和 Array#filter
.
function common(a, b) {
return b.filter(Set.prototype.has.bind(new Set(a)));
}
console.log([[1, 2, 3, 4, 5], [1, 2, 3, 4], [4], [4, 5]].reduce(common));
关于javascript - Javascript 中的数组相交,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51342120/