当前位置:网站首页>Recognize map

Recognize map

2022-06-26 00:57:00 ivanfor666

/* * Map *  * * */
let myMap = new Map();
let keyObj = {};
myMap.set(keyObj, " Sum key keyObj The value of the Association ");
myMap.size;
// 1 myMap.get(keyObj);       
// " Sum key keyObj The value of the Association " myMap.get({});           
// undefined,  because keyObj !== {}     
//  Traverse Map 
for (var [key, value] of myMap) {
    console.log(key + " = " + value);
}
for (var key of myMap.keys()) {
    console.log(key);
}
for (let value of myMap.values()) {
    console.log(value);
}
Array.from(myMap); // [[key,value]] Array.from(myMap.keys())   
//  Copy or merge  Maps 
let one = new Map([[1, 'one'], [2, 'two'], [3, 'three'],]);
new Map(one);
//  Shallow clone   
let first = new Map([[1, 'one'], [2, 'two'], [3, 'three'],]);
let second = new Map([[1, 'uno'], [2, 'dos']]);
//  Merge two Map Object time , If there are duplicate key values , Then the latter will cover the former . 
//  The expansion operator is essentially to expand Map Object to array . 
let merged = new Map([...first, ...second]);
console.log(merged.get(1));
// uno console.log(merged.get(2)); 
// dos console.log(merged.get(3)); // three  
//  Please note that ! by Map It's also possible to set object properties , But it can cause a lot of confusion . 
let wrongMap = new Map()
wrongMap['bla'] = 'blaa'
wrongMap['bla2'] = 'blaaa2'
console.log(wrongMap)  // Map { bla: 'blaa', bla2: 'blaaa2' }  
// Map  Medium  key  Is ordered . therefore , When iterating , One  Map  In the order in which the objects are inserted .   
//  utilize Map  duplicate removal   
var map = new Map;

[1, 1, 22, 33, 22, 33, 11, 1].forEach(item => map.set(item, true));
map.keys()

原网站

版权声明
本文为[ivanfor666]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/177/202206252240330078.html