1.实现并集、交集、差集
<script type="text/javascript">
var set1 = new Set([1,2,3]);
var set2 = new Set([2,3,4,5,6]);
let union = new Set([...set1,...set2]);
let intersect = new Set([...set1].filter(x => set2.has(x)));
let difference = new Set([...set1].filter(x => !set2.has(x)));
console.log(union);//{1,2,3,4,5,6}
console.log(intersect);//{2,3}
console.log(difference);//{1}
</script>
2.实现数组去重
<script type="text/javascript">
//法一:
function dedupe(array) {
return Array.from(new Set(array));
}
console.log(dedupe([1,2,3,3]));//[1,2,3]
//法二:
let arr = [1,2,3,3];
let unique = [...new Set(arr)];
console.log(unique);//[1,2,3]
</script>