es6 语法 (set 和 map)
Posted 小角色Byme
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了es6 语法 (set 和 map)相关的知识,希望对你有一定的参考价值。
{ let list = new Set(); list.add(5); list.add(7); console.log(‘size‘, list, list.size); //{5, 7} 2 } { let arr = [1, 2, 3, 4, 5]; let list = new Set(arr); console.log(‘size‘, list.size); //5 } { let list = new Set(); list.add(1); list.add(2); list.add(1); console.log(‘list‘, list); //{1,2} let arr = [1, 2, 3, 1, ‘2‘]; let list2 = new Set(arr); console.log(‘unique‘, list2); //{1,2,3,‘2‘} } { let arr = [‘add‘, ‘delete‘, ‘clear‘, ‘has‘]; let list = new Set(arr); console.log(‘has‘, list.has(‘add‘)); //true console.log(‘delete‘, list.delete(‘add‘), list); //{"delete", "clear", "has"} list.clear(); //清空 console.log(‘list‘, list); //{} } { let arr = [‘add‘, ‘delete‘, ‘clear‘, ‘has‘]; let list = new Set(arr); console.log(‘list‘, list); //{"add":"add","delete":"delete","clear":"clear","has":"has"} for (let key of list.keys()) { console.log(‘keys‘, key); //add delete clear has } for (let value of list.values()) { console.log(‘value‘, value); //add delete clear has } for (let [key, value] of list.entries()) { console.log(‘entries‘, key, value); // add add;delete delete;clear clear;has has; } list.forEach(function(item) { console.log(‘item‘, item); }) //add delete clear has } { let weakList = new WeakSet(); let arg = {}; weakList.add(arg); // weakList.add(2); console.log(‘weakList‘, weakList); //{} } { let map = new Map(); console.log(‘mapfirst‘, map); //{} let arr = [‘123‘]; map.set(arr, 456); console.log(‘maptwo‘, map);//{array(1)=>456} console.log(‘map‘, map, map.get(arr)); //456 } { let map = new Map([ [‘a‘, 123], [‘b‘, 456] ]); console.log(‘map args‘, map);//{"a"=>123,"b"=>456} console.log(‘size‘, map.size);//2 console.log(‘delete‘, map.delete(‘a‘), map);//true , {"b"=>456} console.log(‘clear‘, map.clear(), map);//{} } { let weakmap = new WeakMap(); let o = {}; weakmap.set(o, 123); console.log(weakmap.get(o));//123 }
以上是关于es6 语法 (set 和 map)的主要内容,如果未能解决你的问题,请参考以下文章