Map 对象保留键值对。任何值(对象或许原始值) 都能够作为一个键或一个值。
运用映照对象
let myMap=new Map();
let keyObj={},keyfunc=function(){},keysTring='hello word';
set(key,value) 添加值
myMap.set(keysTring,'keysTring的关联值')
myMap.set(keyfunc,'keyfunc的关联值')
myMap.set(keyObj,'keyObj的关联值')
size 猎取Map的大小
console.log(myMap.size) //3
get(key) 猎取map的值
console.log(myMap.get(keysTring)) //keysTring的关联值
console.log(myMap.get('hello word')) //keysTring的关联值
----------
console.log(myMap.get(keyfunc)) //keyfunc的关联值
console.log(function(){}===keyfunc) //false
console.log(myMap.get(function(){})) //undefined, 由于keyFunc !== function () {}
----------
console.log(myMap.get(keyObj)) //keyObj的关联值
console.log({}===keyObj) //false
console.log(myMap.get({})) //undefined 由于keyObj !== {}
将NaN作为映照的键
myMap.set(NaN, "not a number");
console.log(myMap.get(NaN)) //not a number
var otherNaN=Number('foo');
console.log(otherNaN) //NaN
console.log(myMap.get(otherNaN)) //not a number 也能够取值
映照与数组对象的关联
let kvArray = [["key1", "value1"], ["key2", "value2"]];
var myMap1 = new Map(kvArray);
//运用映照对象通例的组织函数将一个二维键值对数组对象转换成一个映照关联
console.log(myMap1) //Map { 'key1' => 'value1', 'key2' => 'value2' }
console.log(myMap1.get('key1')) //value1
console.log([...myMap1]) //[ [ 'key1', 'value1' ], [ 'key2', 'value2' ] ]
forEach()
var myMap2 = new Map(kvArray);
myMap2.forEach((value,index)=>{
console.log(value+'---'+index) //value1---key1 value2---key2
})
for..of
var myMap3 = new Map(kvArray);
for(var a of myMap3){
console.log(a) //[ 'key1', 'value1' ] [ 'key2', 'value2' ]
}
for(var [key,value] of myMap3){
console.log('key:'+key+', value:'+value) //key:key1, value:value1 key:key2, value:value2
}
clear()要领会移除Map对象中的一切元素。
let clearMap=new Map();
clearMap.set('hello','word');
console.log(clearMap.size) //1
clearMap.clear(); //清空
console.log(clearMap.size) //0
delete(key)用于移除 Map 对象中指定的元素。
let delMap=new Map();
delMap.set('hi','word');
delMap.set('hello','word');
console.log(delMap) //Map { 'hi' => 'word', 'hello' => 'word' }
delMap.delete('hi'); //实行删除操纵
console.log(delMap) //Map { 'hello' => 'word' }
has(key) 返回一个bool值,用来表明map 中是不是存在指定元素.
let hasMap=new Map();
hasMap.set('hi','word');
hasMap.set('hi','hello word');
console.log(hasMap) //Map { 'hi' => 'hello word' } 一样的key背面的value会掩盖前面的值
console.log(hasMap.has('hi')); //true
console.log(hasMap.has('hello')); //false
entries()把map对象转换为迭代器
let entriesMap=new Map();
entriesMap.set('a','A');
entriesMap.set('b','B');
let entries=entriesMap.entries();
console.log(entries.next()); //{ value: [ 'a', 'A' ], done: false }
console.log(entries.next().value); //[ 'b', 'B' ]
console.log(entries.next()); //{ value: undefined, done: true }
values() 返回一个新的Iterator对象
let valueMap=new Map();
valueMap.set('a','A');
valueMap.set('b','B');
let values=valueMap.values();
console.log(values.next()) //{ value: 'A', done: false }
console.log(values.next()) //{ value: 'B', done: false }
console.log(values.next()) //{ value: undefined, done: true }
WeakMap 对象是一组键/值对的鸠合,个中的键是弱援用的。其键必需是对象,而值能够是恣意的。
WeakMap只提供了4个要领:
1.delete(key); 依据key删除value
2.get(key);依据key猎取value
3.has(key) 依据key搜检是不是是存在value
4.set(key,value) 依据key设置value
let weakMap=new WeakMap();
weakMap.set('hi','word') //非常 由于WeakMap的键只能是个对象
let obj1={};
weakMap.set(obj1,'word') //一般
console.log(weakMap) //WeakMap {}
console.log(weakMap.get(obj1)) //word
console.log(weakMap.has(obj1)) //true
console.log(weakMap.delete(obj1)) //true
天天提高一点,愿望人人喜好,也愿望喜好的朋侪点个赞,后续继承更新…