JS Set traversal

 JavaScript Set only has the key name, or the key and the value are the same

By the way, JS can use this online editing and debugging, which is great

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"

 

Guess you like

Origin blog.csdn.net/qq_38238041/article/details/88870284