数组去重
数组去重采用es6新增Set结构和扩展运算符
const arr = [1,2,3,4,3,2]
const norepeatArr = [...new Set(arr)] // [1,2,3,4]
交集
求两个数组的交集,返回两个数组的重合部分
const arr1 = [1,2,3,4]
const arr2 = [3,4,5,6]
const intersectionArr = arr1.filter(item=>arr2.includes(item)) // [3,4]
并集
求两个数组的并集
const arr1 = [1,2,3,4]
const arr2 = [3,4,5,6]
const unionArr= [...new Set([...arr1,...arr2])] // [1,2,3,4,5,6]
补集
补集即是对另外一个数组的差集
const arr1 = [1,2,3,4]
const arr2 = [3,4,5,6]
const arr1Differ = arr1.filter(item=>!arr2.includes(item)) // [1,2]
const arr2Differ = arr2.filter(item=>!arr1.includes(item)) // [5,6]