new Map() uses

1. What is Map

The Map type is an ordered list of key-value pairs, and both keys and values ​​can be of any type

The difference between Map and Set
Set is a data structure called a set, and Map is a data structure called a dictionary

A collection ----- is a combination of a bunch of unordered, associated, and non-repeating memory structures [called elements in mathematics]. A dictionary
----- is a collection of some elements. Each element has a field called key, and the keys of different elements are different

The Set collection stores elements in the form of [value, value], and
the Map dictionary stores elements in the form of [key, value]

2. CRUD
2.1 size
The size attribute returns the total number of members of the Map structure.

const map = new Map();
map.set(‘foo’, true);
map.set(‘bar’, false);

map.size // 2

2.2 set()
sets the key value corresponding to the key name key to value, and then returns the entire Map structure

If the key already has a value, the key value will be updated, otherwise the key will be newly generated

At the same time, the current Map object is returned, which can be written in chain

const m = new Map();
let fn = function(){
    
    }

m.set('edition', 6)        // 键是字符串
m.set(fn, 'standard')     // 键是函数
m.set(undefined, 'nah')    // 键是 undefined
m.set(1, 'a').set(2, 'b').set(3, 'c') // 链式操作

2.3 get()
The get method reads the key value corresponding to the key. If the key cannot be found, it returns undefined

const m = new Map();

const hello = function() {
    
    console.log('hello');};
m.set(hello, 'Hello ES6!') // 键是函数

m.get(hello)  // Hello ES6!

2.4 has()
The has method returns a Boolean value indicating whether a key is in the current Map object

const hashMap = new Map();

hashMap.set('edition', 6);
hashMap.set(262, 'standard');
hashMap.set(undefined, 'nah');

hashMap.has('edition')     // true
hashMap.has('years')       // false
hashMap.has(undefined)     // true

2.4 delete()
The delete method deletes a key and returns true. Returns false if deletion failed

const m = new Map();
m.set(undefined, 'nah');
m.has(undefined)     // true

m.delete(undefined)
m.has(undefined)       // false

2.5 clear()
The clear method clears all members and has no return value

let map = new Map();
map.set('wife', true);
map.set('car', false);

map.size // 2
map.clear()
map.size // 0

3. Traverse

keys():返回键名的遍历器
values():返回键值的遍历器
entries():返回所有成员的遍历器
forEach():遍历 Map 的所有成员
const map = new Map([
  ['A', 'no'],
  ['B',  'yes'],
]);

//keys()
for (let key of map.keys()) {
    
    
  console.log(key);       // "A"  "B"
}

//values()
for (let value of map.values()) {
    
    
  console.log(value);    // "no"  "yes"
}

// entries()
for (let item of map.entries()) {
    
    
  console.log(item[0], item[1]);
}
// "A" "no"  、"B" "yes"

// 或者
for (let [key, value] of map.entries()) {
    
    
  console.log(key, value);
}
// "A" "no" 、"B" "yes"

// 等同于使用map.entries()
for (let [key, value] of map) {
    
    
  console.log(key, value);
}
// "A" "no"  、 "B" "yes"

map.forEach(function(value, key, map) {
    
    
  console.log(key, value);    // "A" "no"  、 "B" "yes"
});

Guess you like

Origin blog.csdn.net/weixin_47818125/article/details/125286367