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 key1v2 key2v3 key3*/先输出value,再输出keyfor…of…for(let
·
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
*/
更多推荐
已为社区贡献3条内容
所有评论(0)