JavaScript knowledge finishing (4) Map and Set

JavaScript knowledge finishing (4) Map and Set

table of Contents

JavaScript knowledge finishing (4) Map and Set

1. Map  

1, js creates a map object

2. Put the key-value pair into the map object

3. Get the map value according to the key

4. Delete the map specified object

5. Loop through the map

Two, Set


1. Map  

Store a collection of key-value pairs, a dictionary

1, js creates a map object

var map = new Map();

2. Put the key-value pair into the map object

map.set("key",value)

map.set("key1",value1)

map.set("key2",value2)

3. Get the map value according to the key

map.get(key)

4. Delete the map specified object

delete map[key]
map.delete(key)

5. Loop through the map

map.forEach(function(key){
  console.log("key",key)  //输出的是map中的value值
})

Two, Set

Store disordered and unique elements.

var m=new Map();

add(value) :向集合添加一个新的值
delete(value) : 删除集合中的一个值
has(value) : 检测一个值是否在集合中 返回 true/false
clear() :清空集合
size() : 返回集合的数量
values() : 返回一个包含所有值的数组

 

 

Guess you like

Origin blog.csdn.net/qq_41459262/article/details/113935091