我有两个这样的数组:
var x = ['1','2','6'];
var y = ['4', '5','6'];
如何在纯JavaScript中找到两个数组中的重复项,我想避免使用循环?
输出 - duplicates: 6
答案 0 :(得分:2)
尝试这样的事情:
var x = ['1','2','6'];
var y = ['4', '5','6'];
var overlap = x.filter(function(v,i,a){
return y.indexOf(v) > -1;
});
console.log(overlap); // ['6']
这是否适用于您的目的?
答案 1 :(得分:1)
试试这个
var x = ['1','2','6'];
var y = ['4', '5','6'];
var duplicate = [];
for (var i=0; i<y.length; i++) {
var index = x.indexOf(y[i]);
if (index > -1) {
duplicate.push(x[index]);
}
}
Output: ["6"]