javascript - ES6 集中的键和值是否相同?

标签 javascript ecmascript-6 set

我刚刚浏览了 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 [...].

MDN docs

所以不,Sets 根本没有键,但是 .entries() 让您相信在 Maps 和 Sets 之间具有一致性。

关于javascript - ES6 集中的键和值是否相同?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47100017/

相关文章:

javascript - JS/ES6 : Object to Array conversion

javascript - 为什么箭头函数没有参数数组?

java - 带有列表作为单个键值的属性文件

mysql - 给定Java Set中的一组PK,如何选择集合中具有PK的所有行

javascript - 如何更新到 addEventListener?

javascript - 检测 PDF 文档的结尾

javascript - 是否可以包装异步 JS 函数并在 OCaml 中使用它?

javascript - 如何删除JS中的属性并映射新对象?

c# - SetValue 测试在我的 Period 类上成功但在我的 Name 类上失败

javascript - React 代码 "Target container is not a DOM element."的问题