欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

es6set常见实用用法解析

程序员文章站 2023-12-12 23:40:58
并集 //let setofwords1 = new set([1, 2, 3]); //let setofwords2 = new set([3, 5, 2]); var array1 = [...

并集
//let setofwords1 = new set([1, 2, 3]);
//let setofwords2 = new set([3, 5, 2]);

var array1 = [5,6,7,8,18,9,90,81]; 
var array2 = [10,415,54,87,98,65,81];
let a = new set(array1);
let b = new set(array2); 
// 并集
let unionset = new set([...a, ...b]);
// set转化为数组
let unionset1 = array.from(unionset);
console.log(unionset1);
// 交集
let intersectionset = new set([...a].filter(x => b.has(x)));
// set转化为数组
let intersectionset1 = array.from(intersectionset);
console.log(intersectionset1);
// 差集
let differenceabset = new set(
.filter(x => !b.has(x)));
// set转化为数组
let differenceabset1 = array.from(differenceabset);
console.log(differenceabset1);

set 转为数组的另外一种方式
[...a] a 是一个set 对象

上一篇:

下一篇: