JavaScript给定两个数组,写一个方法来计算它们的交集
![](/i/l/?n=22&i=blog/1462173/202210/1462173-20221020234122818-1322206490.png)
查看代码
var nums1 = [1, 2, 2, 1,0],
nums2 = [2, 2, 3, 4,0];
// 1.
// 有个问题, [NaN].indexOf(NaN) === -1
var newArr1 = nums1.filter(function (item) {
return nums2.indexOf(item) > -1;
});
console.log(newArr1);
// 2.
var newArr2 = nums1.filter((item) => {
return nums2.includes(item);
});
console.log(newArr2);
参考GitHub:https://github.com/Advanced-Frontend/Daily-Interview-Question/issues
标签:交集,js,item,给定,var,nums1,nums2 From: https://www.cnblogs.com/mahmud/p/16812012.html