数组常用方法
面试题
求两个数组的交集和差集
交集
const arr1 = [1, 3, 6, 8, 9]
const arr2 = [9, 3]
const result = arr1.filter((v) => arr2.includes(v))
console.log(result) // [3,9]
差集
const arr1 = [1, 3, 6, 8, 9]
const arr2 = [9, 3]
const result2 = arr1.concat(arr2).filter((v) => !arr1.includes(v) || !arr2.includes(v))
console.log(result) // [1,6,8]
网友评论