Map objects hold key-value pairs. Any value (object or raw value) can be a key or a value one. Key can be a string. Key can be an object. Key can be a function. Key can be NaN 2. An iteration in a Map traverses the Map with the following two superlatives. 1.for… of
var obj = {
name: "Chu Wanning".age: 32.action: function() {
console.log("Ink burning")}}for(var key in obj) {
console.log(obj[key])
/ / chu rather late
/ / 32
/ / ƒ () {
// console.log(" ink burning ")
/ /}
// }
Copy the code
2.forEach()
var myMap = new Map(a);The set() method adds or updates a (new) key-value pair to the Map object with a specified key and value.
myMap.set(0."zero");
myMap.set(1."one");
// Two logs will be displayed. One is "0 = zero" and one is "1 = one"
myMap.forEach(function (value, key) {
console.log(key + "=" + value);
// 0 = zero
// 1 = one
}, myMap)
Copy the code
Operations on Map objects 1.Map and Array conversion
var kvArray = [
["key1"."value1"],
["key2"."value2"]].var myMap = new Map(kvArray);
console.log(myMap);
The Map constructor converts a two-dimensional array of key-value pairs into a Map object
var outArray = Array.from(myMap);
console.log(outArray);
The array. from function converts a Map object into a two-dimensional Array of key-value pairs
Copy the code
2. The Map of cloning
var myMap1 = new Map([["key1"."value1"],
["key2"."value2"]]);var myMap2 = new Map(myMap1);
console.log(original === clone);
// Print false. The Map object constructor generates instances and iterates out new objects.
Copy the code
3. The consolidation of the Map
var first = new Map([[1.'one'],
[2.'two'],
[3.'three']]);var second = new Map([[1.'uno'],
[2.'dos']]);Uno, DOS, three; // If two maps have duplicate keys, the last one overwrites the previous one
var merged = new Map([...first, ...second]);
Copy the code