map、weakmap
知识点一:map
1、作用:存储键值对
let map = new Map([[key,value],[key2,value2]]) map.set(key,value) map.get(key) map.delete(key) map.has(key) map.clear() // map中key可以是任意的属性,比如数字、字符串、对象、bool...
2、常用的API
- set
- get
- delete
- has
- clear
map.set(3, '数字'); console.log(map); let obj = { name: 'zs' }; map.set(obj, '对象类型'); console.log(map); let symbol = Symbol('symbol'); map.set(symbol, 'symbol类型'); console.log(map); map.set(true, 'bool'); console.log(map); console.log(map.size); console.log(map.has(3)); map.delete(3); console.log(map);
map.clear();
consoole.log(map);
3、遍历map:forEach()遍历
let map = new Map([['键1','值1'],['键2', '值2'],['键3', '值3'],['键4', '值4']]); console.log(map.entries()); console.log(map.keys()); console.log(map.values()); map.forEach((values, keys) => { console.log(values); })
知识点二:weakmap
1、WeakMap的键只能是引用类型
2、使用new WeakMap()创建
3、常用API
- set
- get
- delete
- has
- clear
let weakMap = new WeakMap([[{ name: 'zs' }, "值1"], [{ age: 18 }, "值2"]]); console.log(weakMap);