What is a Map?
In my opinion, Map is like an advanced version of Object.
Map:
Object:
Map features?
- The Map contains no keys by default; all keys are added by the developer. Unlike the Object prototype chain, which has some default keys.
- Map keys can be any type of data, even functions. The key of Object can only be String or Symbol.
- The number of Map key-value pairs can be easily obtained by using the size property, whereas Object needs to be calculated manually.
- Map performs better than Object when key and value pairs are frequently added and deleted.
When to use Map?
- When you want to add a key value that conflicts with the default key name on Object and you don’t want to rename it, you can use Map instead.
- Use Map when you need data types other than String and Symbol for key values.
- There are many key-value pairs. If you need to count the number, use Map.
- Map is used when key pairs need to be added or deleted frequently.
The Map of grammar?
New:
let map=new Map();
Add:
Incremented one by one: map.set(‘ key 1′,’ key 1′); Map. set(‘ key 2′,’ key 2′); Map. set(‘ key 3′,’ key 3′); Map.set (‘ key 4′,’ key 4′);
Batch increment: 2d key-value array to Map
let kvArray = [["key1"."value1"], ["key2"."value2"]].let myMap = new Map(kvArray);
Copy the code
Delete:
Clear () Delete a key/value pair: map.delete(‘ key name 4’)
Change:
Map.set (‘ key 1′,’ changed key 1′) (the same key name will be replaced automatically)
Check:
Map. get(‘ keyname 1’); // ‘change key value 1’
Has (‘ key name 1’) // true map.has(‘ key name 12’) // false
Find all keys: map.keys(); // MapIterator {” keyname 1″, “keyname 2″,” keyname 3″} returns a new Iterator containing the keys of each element in the Map in insertion order.
const map1 = new Map(a); map1.set('0'.'foo');
map1.set(1.'bar');
const iterator1 = map1.keys();
console.log(iterator1.next().value);
// expected output: "0"
console.log(iterator1.next().value);
// expected output: 1
Copy the code
Query all key values: map.values(); //MapIterator {” changed key 1″, “key 2″,” key 3″}
Use for.. The of method iterates Map:
for (let [key, value] of map) {
console.log(key + "=" + value);
}
/* Output: key name 1 = changed key value 1 Key name 2 = key value 2 Key name 3 = key value 3*/
for (let key of map.keys()) {
console.log(key);
}
Key name 1 key name 2 key name 3 */
Copy the code
Use forEach() to iterate over a Map:
// Note: this is not a write error, the argument is the key value first, the key name after
map.forEach(function(value, key) {
console.log(key + "=" + value);
})
/* Output: key name 1 = changed key value 1 Key name 2 = key value 2 Key name 3 = key value 3*/
Copy the code
Map to array
1 / / method
Array.from(map);
/ * [[" 1 ", "change the key value 1"], [" key name 2 ", "key value 2"], [" key name 3 ", "three key"]] * /
2 / / method
[...map]
/ * [[" 1 ", "change the key value 1"], [" key name 2 ", "key value 2"], [" key name 3 ", "three key"]] * /
Copy the code
The Map merging
let first = new Map([[1.'one'],
[2.'two'],
[3.'three']]);let second = new Map([[1.'uno'],
[2.'dos']]);// When two maps are merged, if there are duplicate keys, the latter overwrites the former.
// The expansion operator essentially converts a Map object into an 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
Copy the code
Map objects can also be merged with arrays:
// When a Map object is merged with an array, if there are duplicate keys, the last one overwrites the previous one.
let merged = new Map([...first, ...second, [1.'eins']]);
console.log(merged.get(1)); // eins
Copy the code