JavaScript knowledge finishing (4) Map and Set
table of Contents
JavaScript knowledge finishing (4) Map and Set
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
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() : 返回一个包含所有值的数组