Map Set iterator
Map
var map=new Map([['tom',100],['sam',90],['alice',80]]);//键值对
var name=map.get('tom');//通过key获得value
console.log(name);
//输出100
map.set('vvv',123);//通过set设置或修改值
map
//Map(4) {'tom' => 100, 'sam' => 90, 'alice' => 80, 'vvv' => 123}
map.delete("sam");//删除
Set
无需不重复的集合
var set=new Set([2,1,1,1,1]);
set
//输出Set(2) {2, 1} set可以去重
//add
set.add(5);
Set(3) {2, 1, 5}
//delete
set.delete(1);
true
set
Set(2) {2, 5}
//判断某个元素是否在set中 set.has()
console.log(set.has(5));
//true
iterator
遍历map
var map=new Map([['tom',100],['sam',90],['alice',80]]);
for(let x of map){
console.log(x);
}
/*(2) ['tom', 100]0: "tom"1: 100length: 2[[Prototype]]: Array(0)
(2) ['sam', 90]0: "sam"1: 90length: 2[[Prototype]]: Array(0)
(2) ['alice', 80]0: "alice"1: 80length: 2[[Prototype]]: Array(0)*/
遍历set(同理)

浙公网安备 33010602011771号