1 //Set 2 { 3 let list=new Set(); 4 list.add(5);//添加 5 list.add(7); 6 //属性size就是长度 7 console.log('size',list.size);//2 8 } 9 {10 let arr = [1,2,3,4,5];11 let list = new Set(arr);12 console.log(list.size);//513 }14 {15 //去重16 let list = new Set();17 list.add(1);18 list.add(2);19 list.add(1);20 console.log('size',list);//1,2,没有最后的121 22 let arr=[1,2,3,1,2];23 let list2=new Set(arr);24 console.log('unique',list2);//1,2,3,没有后面的1,225 }26 {27 let arr=['add','delete','clear','has'];28 let list=new Set(arr);29 console.log('has',list.has('add'));//查看是否存在30 console.log('delete',list.delete('add'),list);//删除31 console.log('clear',list.clear('add'),list);//清空32 }33 //遍历34 {35 let arr=['add','delete','clear','has'];36 let list=new Set(arr);37 38 for(let key of list.keys()){39 console.log('key',key);40 }41 for(let value of list.values()){42 console.log('keys',value);43 }44 for(let [key,value] of list.entries()){45 console.log('entries',key,value);46 }47 list.forEach(function(item){48 console.log(item)49 })50 }51 //WeakSet:元素只能是对象,不能是数值,不能遍历,没有clear方法,没有set属性52 {53 let weakList=new WeakSet();54 let arg={};55 weakList.add(arg);56 //weakList.add(2);不能是数值57 console.log('weakList',weakList);58 }59 //Map,遍历和set一样60 {61 let map= new Map();62 let arr=['123'];63 map.set(arr,456);//添加元素(key,value)64 console.log('map',map,map.get(arr));//get(key)获取值65 }66 {67 let map= new Map([['a',123],['b',456]])68 console.log(map,map.size)69 console.log(map.delete('a'),map)70 console.log(map.clear(),map)71 }72 //同WeakSet73 {74 let weakmap=new WeakMap();75 }