JS中Map的遍历

1.forEach遍历

const map = new Map([['key1','v1'],['key2','v2'],['key3','v3']]);
console.log(map);
map.forEach((val,key) => {
  console.log(val,key);
})
/**
v1 key1
v2 key2
v3 key3 
 */

先输出value,再输出key

for…of…

for(let i of map) {
  console.log(i);
}
/**
[ 'key1', 'v1' ]
[ 'key2', 'v2' ]
[ 'key3', 'v3' ] 
 */

输出数组格式

for…of…搭配数组解构

//搭配解构赋值
for(let [key,val] of map) {
  console.log(key,val);
}
/**
key1 v1
key2 v2
key3 v3
 */

你可能感兴趣的:(前端,javascript,前端,javascript,es6)