ES5的写法
var a = [1,2,3,4,5]
var b = [2,4,6,8,10]
// //交集
var c = a.filter(function(v){ return b.indexOf(v) > -1 })
// //差集
var d = a.filter(function(v){ return b.indexOf(v) == -1 })
// //补集
var e = a.filter(function(v){ return !(b.indexOf(v) > -1) })
.concat(b.filter(function(v){ return !(a.indexOf(v) > -1)}))
//并集
var f = a.concat(b.filter(function(v){ return !(a.indexOf(v) > -1)}));
console.log("数组a:", a);
console.log("数组b:", b);
console.log("a与b的交集:", c);
console.log("a与b的差集:", d);
console.log("a与b的补集:", e);
console.log("a与b的并集:", f);
用ES6 的写法
var a = [1,2,3,4,5] var b = [2,4,6,8,10] console.log("数组a:", a); console.log("数组b:", b); var sa = new Set(a); var 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);
原文地址:https://www.cnblogs.com/buxiugangzi/p/11563488.html
时间: 2024-11-09 00:45:52