gpt4 book ai didi

javascript - 如何访问没有键的对象,只有值javascript

转载 作者:行者123 更新时间:2023-11-30 11:10:10 25 4
gpt4 key购买 nike

var hasDuplicates = "eyes";
var noDuplicates = new Set(hasDuplicates); // {"e", "y", "s"}


console.log(Object.keys(noDuplicates)); // []
console.log(Object.values(noDuplicates)); // []

我基本上想访问名为“noDuplicates”的集合的“e”、“y”和“s”。

var setToArray = [];
for (spot in noDuplicates) {
setToArray.push(Object.keys(noDuplicates)[spot])
}

最佳答案

您可以使用 array spread syntax将 Set 转换为数组:

var hasDuplicates = "eyes";
var noDuplicates = new Set(hasDuplicates); // {"e", "y", "s"}
var setToArray = [...noDuplicates];

console.log(setToArray);

您还可以使用 Set.forEach()for...of loop直接访问 Set 的值:

var hasDuplicates = "eyes";
var noDuplicates = new Set(hasDuplicates); // {"e", "y", "s"}

noDuplicates.forEach(v => console.log(v));

for(const v of noDuplicates) {
console.log(v);
}

关于javascript - 如何访问没有键的对象,只有值javascript,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54013908/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com