乐趣区

es6 Set的几种使用场景

// 数组去重
let arr = [1, 1, 2, 3];
let unique = [… new Set(arr)];

let a = new Set([1, 2, 3]);
let b = new Set([4, 3, 2]);

// 并集
let union = […new Set([…a, …b])];

// 交集
let intersect = […new Set([…a].filter(x => b.has(x)))];

// 差集
let difference = Array.from(new Set([…a].filter(x => !b.has(x))));

退出移动版