JS Set遍历
最编程
2024-01-14 21:08:07
...
JavaScript的Set只有键名,或者是说键和值一样
顺便提一下,JS可以使用这个在线编辑调试,非常棒
https://blog.****.net/qq_38238041/article/details/88869133
const set = new Set(['aa','bb','cc']);
// 获取所有key
for(let key of set.keys()) {
console.log(key);
}
// "aa"
// "bb"
// "cc"
// 获取所有value
for(let val of set.values()) {
console.log(val);
}
// "aa"
// "bb"
// "cc"
// key and value
for(let item of set.entries()) {
console.log(item);
}
// ["aa", "aa"]
// ["bb", "bb"]
// ["cc", "cc"]
// 我习惯的方式
for(let item of set) {
console.log(item);
}
// "aa"
// "bb"
// "cc"
// 更方便的操作
set.forEach((key,val) => console.log(key + ": " + val))
// "aa: aa"
// "bb: bb"
// "cc: cc"
上一篇: 用 for in 遍历前端对象的方法
推荐阅读