ES6 15.Set数组集合(ES6 15.set array collection)

Set数据集合

一、Set数据集合

1.ES6之前只有数组一种数据结构,现在提供Set和Map两种集合

2.Set集合是一种无重复元素的列表,使用new Set()方法创建Set集合

// 创建一个Set集合
let set=new Set();
set.add(1);
set.add(2);
set.add(2);
set.add('2');
set.add('c');
console.log(set);    //Set(4) { 1, 2, '2', 'c' }
console.log(set.size);  //4

ps:向集合添加五个元素,只有四个有效,并且2和字符串  ‘2’ 不冲突,空对象也不冲突

3.我们也可以通过构造函数传递参数的方式进行初始化集合,比如接受一个数组

// 通过构造参数初始化集合
let set =new Set([1,2,2,3,3,4,5]);
console.log(set); //Set(5) { 1, 2, 3, 4, 5 }

 4.使用has()方法查找是否存在指定元素,注意2和  ‘2’  是两个元素,不会隐形转换‘’

console.log(set.has(2));  //true
console.log(set.has('2')); //false

5.使用delete()删除指定元素、clear()清除元素

set.delete(2);  //删除元素
console.log(set); //Set(4) { 1, 3, 4, 5 }
set.clear(); //清空
console.log(set); //Set(0) {}

6.我们可以使用  . . . 语法,将Set集合转换为数组

// 将set集合转换成数组
let array=[...set];
console.log(array);  //[ 1, 2, 3, 4, 5 ]

7.我们可以使用for或者forEach来遍历Set集合

// for...of遍历
for(let i of set){
   console.log(i);
}  
//forEach变量
//在Set集合中key和value都是值
// s表示set集合本身
set.forEach(function(key,value,s){
    console.log(key+'-'+value);
    console.log(s);
})

8.Set集合还提供针对对象的Weak Set集合,添加非对象集合类型会报错

9.Weak Set 集合支持add() 、has() 和delete()方法

10.Weak Set 不支持遍历,内部隐藏(无法查看内容),不支持foreach和size

11.对应用场景来说,存放对象的弱引用,不用担心对象被回收后引发的问题

// 强引用
let set =new Set(),
   obj={1:1};

   set.add(obj);
   console.log(set);//引用存在
//    移出引用
obj=null;
console.log(set); //引用依然存在

// 弱引用
let ws=new WeakSet(),
  obj={1:1};

ws.add(obj);
ws.add(global);
console.log(ws.has(global));
console.log(ws.has(obj)); //true 引用存在

// 移出引用
obj=null;
console.log(ws.has(obj)); //false  随着销毁而释放
————————

Set data set

1、 Set data set

1. Before ES6, there was only one data structure, array. Now set and map are provided

2. A set set is a list of < strong > non repeating elements < / strong >, which is created using the new set() method

// 创建一个Set集合
let set=new Set();
set.add(1);
set.add(2);
set.add(2);
set.add('2');
set.add('c');
console.log(set);    //Set(4) { 1, 2, '2', 'c' }
console.log(set.size);  //4

PS: add five elements to the collection, only four are valid, and 2 and string   ‘ 2 ‘does not conflict, and empty objects do not conflict

3. We can also initialize the collection by passing parameters through the constructor, such as accepting an array

// 通过构造参数初始化集合
let set =new Set([1,2,2,3,3,4,5]);
console.log(set); //Set(5) { 1, 2, 3, 4, 5 }

4. Use the has () method to find out whether the specified element exists. Note 2 and 3   ‘ 2’   It is two elements and will not convert ” invisibly

console.log(set.has(2));  //true
console.log(set.has('2')); //false

5. Use delete() to delete the specified element and clear() to clear the element

set.delete(2);  //删除元素
console.log(set); //Set(4) { 1, 3, 4, 5 }
set.clear(); //清空
console.log(set); //Set(0) {}

6. We can use   . . . Syntax to convert a set collection to an array

// 将set集合转换成数组
let array=[...set];
console.log(array);  //[ 1, 2, 3, 4, 5 ]

7. We can use for or foreach to traverse the set set

// for...of遍历
for(let i of set){
   console.log(i);
}  
//forEach变量
//在Set集合中key和value都是值
// s表示set集合本身
set.forEach(function(key,value,s){
    console.log(key+'-'+value);
    console.log(s);
})

8. The set also provides a weak set set for objects. Adding a non object set type will report an error

9. The weak set supports the add(), has() and delete() methods

10. Weak set does not support traversal, internal hiding (unable to view content), foreach and size

11. For application scenarios, weak references of objects are stored without worrying about problems caused by objects being recycled

// 强引用
let set =new Set(),
   obj={1:1};

   set.add(obj);
   console.log(set);//引用存在
//    移出引用
obj=null;
console.log(set); //引用依然存在

// 弱引用
let ws=new WeakSet(),
  obj={1:1};

ws.add(obj);
ws.add(global);
console.log(ws.has(global));
console.log(ws.has(obj)); //true 引用存在

// 移出引用
obj=null;
console.log(ws.has(obj)); //false  随着销毁而释放