let arr1 = [1, 2, 3, 4, 5, 6, 7];
let arr2 = [4, 5, 6, 7, 8, 9, 0];
let arr3 = arr1.filter(i => arr2.includes(i));
// 或者 let arr3 = arr1.filter(i => arr2.some(j => j == i));
console.log(arr3)
//控制台输出: (4) [4, 5, 6, 7]
//复杂数据类型也可以
let arr1 = [1, 2, 3, 4, 5, 6, 7];
let arr2 = [4, 5, 6, 7, 8, 9, 0];
let arr3 = arr1.filter(i => arr2.includes(i));
// 或者 let arr3 = arr1.filter(i => arr2.some(j => j == i));
console.log(arr3)
//控制台输出: (4) [4, 5, 6, 7]
//复杂数据类型也可以
本文标题:js求2个集合交集再不要写push了
本文链接:https://www.haomeiwen.com/subject/vkzvidtx.html
网友评论