1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
| let a = [1,2,3,4,5] let b = [2,4,6,8,10]
let sa = new Set(a); let sb = new Set(b);
// 交集 let intersect = a.filter(x => sb.has(x));
// 差集 let minus = a.filter(x => !sb.has(x));
// 补集 let complement = [...a.filter(x => !sb.has(x)), ...b.filter(x => !sa.has(x))];
// 并集 let unionSet = Array.from(new Set([...a, ...b]));
console.log("a与b的交集:", intersect); console.log("a与b的差集:", minus); console.log("a与b的补集:", complement); console.log("a与b的并集:", unionSet);
|