1.基础实例
数组
let set1 = new Set([1, 2, 3, 4, 5, 6]);
let set2 = new Set([4, 5, 6, 7, 8, 9]);
let set3 = new Set([4, 5, 9, 17, 18, 19]);
并集
// 并集 所有数组的集合去重
let union = new Set([...set1, ...set2, ...set3]);//三个数组合并并且去重
// let myArr = Array.from(union);//1.转化为数组
var myArr = [...union];//2.转化为数组
console.log(myArr);
交集
// 交集
let intersect = new Set([...set1].filter(x => set2.has(x)));//去除两者公共部分
console.log(intersect);
差集
let intersect1 = new Set([...set1].filter(x => !set2.has(x)));//表示 set1存在的在set2不存在的
let intersect2 = new Set([...set2].filter(x => !set1.has(x)));//表示 set2存在的在set1不存在的
console.log(intersect1);
console.log(intersect2);
网友评论