使用 Set 可以很容易地实现并集(Union)、交集(Intersect)和差集
let a = new Set([1, 2, 3]);
let b = new Set([4, 3, 2]);
// 并集
let union = new Set([...a, ...b]);
// Set {1, 2, 3, 4}
// 交集
let intersect = new Set([...a].filter(x => b.has(x)));
// set {2, 3}
// 差集
let difference = new Set([...a].filter(x => !b.has(x)));
lodash也有相应的方法
// 差集
_.difference([2, 1], [2, 3]);
// => [1]
_.xor([2, 1], [2, 3]);
// => [1, 3]
// 并集
var array = [1];
var other = _.concat(array, 2, [3], [[4]]);
console.log(other);
// => [1, 2, 3, [4]]
// 交集
_.intersection([2, 1], [2, 3]);
// => [2]