set,map
-
set
1 { 2 let list = new Set(); 3 list.add(5);// set添加元素用add 4 list.add(7); 5 console.log('size',list.size) //获取长度2 6 } 7 { 8 //可以给set直接初始化 9 let arr = [1,2,3,4,5]; 10 let list = new Set(arr); 11 console.log('size',list.size) // 5 12 }
set的值是唯一的,应用:数组去重
1 { 2 let list = new Set(); 3 list.add(1); 4 list.add(2); 5 list.add(1); 6 console.log('list',list) 7 8 let arr2 = [1,2,3,2,1]; //字符串不会自动转换,2和‘2’是不一样的值,不会去重 9 let list2 = new Set(arr2); 10 console.log('list2',list2) //1,2,3 11 }
添加,移除,清除,含有
1 { 2 let arr = ['add','delete','clear','has']; 3 let list = new Set(arr); 4 console.log('has',list.has('add')) 5 console.log('delete',list.delete('add'),list) 6 list.clear(); 7 console.log(list) 8 }
遍历
1 { 2 let arr = ['add','delete','clear','has']; 3 let list = new Set(arr); 4 for(let key of list.keys()){ 5 console.log('key',key) 6 } 7 for(let value of list.values()){ 8 console.log('value',value) 9 } 10 for(let [key,value] of list.entries()){ 11 console.log('entries',key,value) 12 } 13 14 list.forEach(item=>{console.log(item)}) 15 }
-
map
1 { 2 // map 3 let map = new Map(); 4 let arr = ['123']; 5 6 map.set(arr,456); //map 添加元素用set,以key,value 添加,key可以是任意类型 7 console.log(map,map.get(arr)) //获取 get 8 } 9 10 { 11 let map = new Map([['a',1],['b',2]]) //注意格式 12 console.log(map) 13 console.log('size',map.size) 14 console.log('delete',map.delete('a'),map) 15 console.log('clear',map.clear(),map) 16 17 }
-
WeakSet,WeakMap
1 { 2 // weakSet 数据只能是对象,是弱引用,不会检测其他地方有没有用到 3 // 没有clear方法,不能遍历,没有size属性 4 let weakList = new WeakSet(); 5 let arg = {}; 6 weakList.add(arg); 7 console.log(weakList) 8 } 9 { 10 // WeakMap 接收的key值只能是对象,没有clear,不能遍历 11 let WeakMapList = new WeakMap(); 12 let o = {}; 13 WeakMapList.set(o,123); 14 console.log(WeakMapList) 15 }
-
map,set与array的对比
map 数组的对比
1 { 2 // 数据结构横向对比,增删改查 3 let map = new Map(); 4 let array = []; 5 // 增 6 map.set('t',1); 7 array.push({t:1}); 8 console.info('增',map,array) 9 10 // 查 11 let map_exist = map.has('t'); //true 12 let array_exist = array.find(item=>item.t); //返回当前值 13 console.info('查',map_exist,array_exist) 14 15 // 改 16 map.set('t',2); 17 array.forEach(item=>item.t?item.t=2:''); //判断如果值存在,则进行修改,不存在不进行修改 18 console.info('改',map,array) 19 20 //删 21 map.delete('t') 22 let index = array.findIndex(item=>item.t); //查找元素所在位置下标 23 array.splice(index,1); //删除指定位置的元素 24 console.info('删',map,array) 25 26 }
set与数组的对比
1 { 2 let set = new Set(); 3 let array= []; 4 // 增 5 set.add({t:1}); 6 array.push({t:1}); 7 console.info('add',set,array) 8 9 //查 10 let setExist = set.has({t:1}); 11 let arrayExist = array.find(item=>item.t) 12 console.info('get',setExist,arrayExist) 13 14 // 改 15 set.forEach(item=>item.t?item.t = 3 : ''); 16 array.forEach(item=>item.t?item.t = 3 :''); 17 console.info('update',set,array) 18 19 // 删 20 set.forEach(item=>item.t?set.delete(item.t):'') 21 let index = array.findIndex(item => item.t); 22 array.splice(index,1); 23 console.info('delete',set,array) 24 }
-
map,set与object的对比
1 { 2 //map set object 对比 3 let item = {t:1}; 4 let map = new Map(); 5 let set = new Set(); 6 let obj = {}; 7 8 // 增 9 map.set('t',1); 10 set.add(item); 11 obj['t'] = 1; 12 13 console.info('add',map,set,obj) 14 15 // 查 16 console.table({ 17 map_exist:map.has('t'), 18 set_exist:set.has(item), 19 obj_exist:'t' in obj //对象用in查找 20 }) 21 22 // 改 23 map.set('t',2); 24 item.t=2; 25 obj['t']=2; 26 console.log('update',map,set,obj) 27 28 // 删 29 map.delete('t'); 30 set.delete(item); 31 delete obj['t']; 32 console.info('delete',map,set,obj) 33 }