我刚刚浏览了 set 的 MDN 文档 ,以及它是如何工作的,谈到如何迭代一个集合的部分,我看到了以下示例:
// logs the items in the order: 1, "some text", {"a": 1, "b": 2}
for (let item of mySet) console.log(item);
和
// logs the items in the order: 1, "some text", {"a": 1, "b": 2}
for (let item of mySet.values()) console.log(item);
和
// logs the items in the order: 1, "some text", {"a": 1, "b": 2}
//(key and value are the same here)
for (let [key, value] of mySet.entries()) console.log(key);
只是确认一下,这是否意味着在使用 set 时键和值是相同的?
最佳答案
The entries() method returns a new Iterator object that contains an array of [value, value] for each element in the Set object, in insertion order [...].
所以不,Sets 根本没有键,但是 .entries()
让您相信在 Maps 和 Sets 之间具有一致性。
关于javascript - ES6 集中的键和值是否相同?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47100017/