js取交集、并集、差集(es6中 set 方法)


let arr1 = new Set([1, 2, 3]);
let arr2 = new Set([4, 3, 2]);
 
// 并集
let union = new Set([...arr1, ...arr2]);
// Set {1, 2, 3, 4}
 
// 交集
let intersect = new Set([...arr1].filter(x => arr12.has(x)));
// set {2, 3}
 
// 差集
let difference = new Set([...arr1].filter(x => !arr2.has(x)));

你可能感兴趣的:(javascript,javascript,html,es6)