Set 对象作用
- 数组去重(利用扩展运算符)
const mySet = new Set([1, 2, 3, 4, 4]) [...mySet] // [1, 2, 3, 4]
合并两个set对象
let a = new Set([1, 2, 3]) let b = new Set([4, 3, 2]) let union = new Set([...a, ...b]) // {1, 2, 3, 4}
交集
let a = new Set([1, 2, 3]) let b = new Set([4, 3, 2]) let intersect = new Set([...a].filter(x => b.has(x))) // {2, 3} 利用数组的filter方法
差集
let a = new Set([1, 2, 3]) let b = new Set([4, 3, 2]) let difference = new Set([...a].filter(x => !b.has(x))) // {1}