//Set
{
let list=new Set();
list.add(5);//添加
list.add(7);
//属性size就是长度
console.log('size',list.size);//
}
{
let arr = [1,2,3,4,5];
let list = new Set(arr);
console.log(list.size);//
}
{
//去重
let list = new Set();
list.add(1);
list.add(2);
list.add(1);
console.log('size',list);//1,2,没有最后的1
let arr=[1,2,3,1,2];
let list2=new Set(arr);
console.log('unique',list2);//1,2,3,没有后面的1,2
}
{
let arr=['add','delete','clear','has'];
let list=new Set(arr);
console.log('has',list.has('add'));//查看是否存在
console.log('delete',list.delete('add'),list);//删除
console.log('clear',list.clear('add'),list);//清空
}
//遍历
{
let arr=['add','delete','clear','has'];
let list=new Set(arr);
for(let key of list.keys()){
console.log('key',key);
}
for(let value of list.values()){
console.log('keys',value);
}
for(let [key,value] of list.entries()){
console.log('entries',key,value);
}
list.forEach(function(item){
console.log(item)
})
}
//WeakSet:元素只能是对象,不能是数值,不能遍历,没有clear方法,没有set属性
{
let weakList=new WeakSet();
let arg={};
weakList.add(arg);
//weakList.add(2);不能是数值
console.log('weakList',weakList);
}
//Map,遍历和set一样
{
let map= new Map();
let arr=['123'];
map.set(arr,456);//添加元素(key,value)
console.log('map',map,map.get(arr));//get(key)获取值
}
{
let map= new Map([['a',123],['b',456]])
console.log(map,map.size)
console.log(map.delete('a'),map)
console.log(map.clear(),map)
}
//同WeakSet
{
let weakmap=new WeakMap();
}