<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<script>
//数组和set集合转换
let set1 = new Set([1, 2, 3, 4,3, 4, 5]);
console.log(set1);
//字符串转集合
let str1 = "1,2,3,4,5,6,7,8,9,10";
let set2 = new Set(str1.split(","));
console.log(set2);
//集合转数组
let arr1 = [...set1];
console.log(arr1);
//数组去重
let arr2 = [1, 2, 3, 4,3, 4, 5];
let rel=[...new set(arr2)];
console.log(rel);
//求并集
let arr3 = [1, 2, 3];
let arr4 = [4, 5, 6];
let union = [...new Set([...arr3,...arr4])];
console.log(union);
//求交集
let arr5 = [1, 2, 3];
let arr6 = [2, 3, 4];
let inter = [...new Set([...arr5].filter(x => arr6.includes(x)))];
console.log(inter);
//求差集
let arr7 = [1, 2, 3];
let arr8 = [2, 3, 4];
let diff = [...new Set([...arr7].filter(x => !arr8.includes(x)))];
console.log(diff);
</script>
</body>
</html>
标签:ES6,Set,console,log,...,运算符,let,new
From: https://www.cnblogs.com/zy8899/p/18447973