1.并集: [A B C R U ]#
```
let set111 = new Set(["A", "B", "C"])
let set222 = new Set(["A", "R", "U"])
let unionset = new Set();
set111.forEach((ele) => {
if (!unionset.has(ele)) {
unionset.add(ele)
}
})
set222.forEach((ele) => {
if (!unionset.has(ele)) {
unionset.add(ele)
}
})
console.log(unionset);
//es6
let union = new Set([...set111, ...set222]);
console.log(union);
```
2.交集: [A ]#
let differtset = new Set();
set111.forEach((ele) => {
if (set222.has(ele)) {
differtset.add(ele)
}
})
console.log(differtset);
//es6
let intersect = new Set([...set111].filter(x => set222.has(x)));
console.log(intersect);
3.差集#
// let set111 = new Set(["A","B","C"])
// let set222 = new Set(["A","R","U"])
let chaset = new Set();
set111.forEach((ele) => {
if (!set222.has(ele)) {
chaset.add(ele)
}
})
console.log(chaset);
//es6
let difference = new Set([...set111].filter(x => !set222.has(x)));
console.log(difference);
4.子集#
// [a] [a,b] 是
let set1 = new Set([“A”, “b”])
let set2 = new Set([“A”, “R”, “U”])
var str = “”
set1.forEach((ele) => {
if (set2.has(ele)) {
str = “true”
} else {
str = “false”
}
})
console.log(str);```