Set的介绍
ES6提供了新的数据结构Set。它类似于数组,但是成员的值都是唯一的,没有重复的值,
是一个集合对象。
Set本身是一个构造函数,用来生成Set数据结构。
Set的使用
初始化
const a = new Set ( ) ;
Set函数可以接受一个数组或多个数组作为参数, 来初始化。
const b = new Set ( [ 1 , 2 , 2 , 3 ] ) ;
console. log ( b) ;
特性
支持for of
支持扩展运算符
属性和方法
size属性是集合的大小
Set原型身上的方法:
add(value)添加值 返回Set本身
delete(value)删除值 返回布尔值
has() 判断是否有值,返回布尔值
clear() 清空Set,没有返回值
forEach(value => console.log(value);) 遍历Set
Set的应用场景
数组去重
let arr = [ 1 , 1 , 2 , 4 , 3 , 5 , 6 , 4 ] ;
console. log ( '数组去重' , [ ... new Set ( arr) ] ) ;
数组取交集
let arr = [ 1 , 1 , 2 , 4 , 3 , 5 , 6 , 4 ] ;
let arr1 = [ 1 , 7 , 3 , 3 , 4 ] ;
let intersection = [ ... new Set ( arr) ] . filter ( function ( item ) {
return new Set ( arr1) . has ( item) ;
} )
console. log ( '交集' , intersection) ;
数组取并集
let arr = [ 1 , 1 , 2 , 4 , 3 , 5 , 6 , 4 ] ;
let arr1 = [ 1 , 7 , 3 , 3 , 4 ] ;
let union = [ ... new Set ( arr, arr1) ] ;
console. log ( '并集' , union) ;
数组取差集
let arr = [ 1 , 1 , 2 , 4 , 3 , 5 , 6 , 4 ] ;
let arr1 = [ 1 , 7 , 3 , 3 , 4 ] ;
let subtraction = [ ... new Set ( arr) ] . filter ( function ( item ) {
return ! new Set ( arr1) . has ( item) ;
} )
console. log ( 'arr差集arr1(也就是arr中有的arr1中没有)' , subtraction) ;
推荐阅读
我在js数据结构专栏有用原生js数组实现一个Set,大家有兴趣可以阅读一下。
js实现Set集合