Make a summary every day, persistence is victory!

    /**
        @date 2021-06-09
        @description ES6之Map
    */
Copy the code

One (sequence)

Map is a new data structure added in ES6. It is similar to Object except that the key name of Object can only be a string. If a reference data type is used as the key name of Object, the reference data type is converted to a string first, whereas Map can use all data types in JavaScript as the key name.

Ii (Use)

Using the Map constructor to declare a new Map object, you can pass a data structure with an Iterator interface, and each member is two-element.

const map = new Map([['a', 1], ['b', 2]]); // Map(2) {"a" => 1, "b" => 2}
Copy the code
const map = new Map(new Set([['a', 1], ['b', 2]])); // Map(2) {"a" => 1, "b" => 2}
Copy the code

Iii (Attributes and Methods)

Map has the following built-in properties and methods: public code:

let map = new Map([['a', 1], ['b', 2]]);
Copy the code
  1. size– Returns the Map objectThe length of the;
map.size; / / 2Copy the code
  1. set(key, value)– Sets the key value of Map object key to value. If the key name already exists in the Map object, the Map object is overwritten. Otherwise, a new Map object is created and the configured Map object is returned.
map.set('a', 3); // Map(2) {"a" => 3, "b" => 2}
map.set('c', 4); // Map(3) {"a" => 3, "b" => 2, "c" => 4}
Copy the code
  1. get(key)– Gets the value corresponding to key in the Map object. If no key exists, undefined is returned.
map.get('a'); // 3
map.get('d'); // undefined
Copy the code
  1. has(key)– Checks whether the key exists in the Map object.
map.has('a'); // true
map.has('d'); // false
Copy the code
  1. delete(key)– Deletes an item of the Map object and displays whether the deletion is successful.
map.delete('a'); // true
map.delete('d'); // false
console.log(map); // Map(2) {"b" => 2, "c" => 4}
Copy the code
  1. clear()emptyMap object, with no return value;
map.clear();
console.log(map); // Map(0) {}
Copy the code
  1. keys()– Returns a Map objectKey nameTraverser;
map.set('name', 'E1e').set('age', 18); // Map(2) {"name" => "E1e", "age" => 18}
Copy the code
for(const key of map.keys()){
    console.log(key); // 'name', 'age'
}
Copy the code
  1. values()– Returns a Map objectThe key valueTraverser;
for(const val of map.values()){
    console.log(val); // 'E1e', 18
}
Copy the code
  1. entries()– Returns a Map objectKey/value pairTraverser;
for(const item of map.entries()){
    console.log(item); // ['name', 'E1e'], ['age', 18]
}
Copy the code
  1. forEach()traverseAll members of a Map object, similar to the forEach method of an array;
map.forEach((val, key, source) => {
    console.log(val, key, source);
})
/**
'E1e' 'name' Map(2) {"name" => "E1e", "age" => 18}
18 'age' Map(2) {"name" => "E1e", "age" => 18}
*/
Copy the code
  1. Using the extended operator (.) convert the Map object to an array;
const mapArr = [...map]; // [['name', 'E1e'], ['age', 18]]
Copy the code

Iv (Extended)

Like Set, Map also corresponds to a data structure, WeakMap. The main differences between WeakMap and Map are as follows:

  1. The key name in WeakMapCan only be objects;
const wm = new WeakMap(); wm.set(true, 1); // Uncaught TypeError: Invalid value used as weak map key wm.set({a: 1}, {b: 2}); / / WeakMap {{... } = > {... }}Copy the code
  1. The reference of key name in WeakMap is the same as the value in WeakSet, isA weak reference, garbage collection mechanism does not consider the reference of key names in WeakMap;
  2. There is no traversal operation in WeakMap and nosizeProperties;
wm.size; // undefined
wm.keys; // undefined
Copy the code