JavaScript/map

来自康健生活
Kangkk​(留言 | 贡献)2021年1月18日 (一) 16:15的版本 (创建页面,内容为“== 引言 == map 对象和 map 集合的遍历的区别是:map 集合是无序的,并且主键是唯一的。集合中的键和值可以是任何类型。 == 遍…”)
(差异) ←上一版本 | 最后版本 (差异) | 下一版本→ (差异)

引言

map 对象和 map 集合的遍历的区别是:map 集合是无序的,并且主键是唯一的。集合中的键和值可以是任何类型。

遍历

forEach

map.forEach(function (item) {
  console.log(item.toString());
});
map.forEach(function(value, key, map){
  console.log(value, key);
})

for-of

for(let item of map){
  console.log(item)
}
for(let item of map.values()){
  console.log(item)
}
for(let item of map.keys()){
  console.log(item)
}
for(let item of map.entries()){
  console.log(item)
}
for (var [key, value] of map) {
  console.log(key, value)
}