JS Set遍历

 JavaScript的Set只有键名,或者是说键和值一样

顺便提一下,JS可以使用这个在线编辑调试,非常棒

https://blog.csdn.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"

猜你喜欢

转载自blog.csdn.net/qq_38238041/article/details/88870284