我在使用 Typescript 2.6 的代码中遇到了以下奇怪的问题。我试图遍历一组字符串值,但出现以下错误,我不明白为什么。
'Type 'Set' is not an array type or a string type. '
这是我的:
loopThroughSet(): void {
let fruitSet = new Set()
.add('APPLE')
.add('ORANGE')
.add('MANGO');
for (let fruit of fruitSet) {
console.log(fruit);
}
}
谁知道问题出在哪里? 提前致谢
最佳答案
Set 没有在 TS 中定义,你需要使用 es2017.object 配置 TS 或将 Set 值转换为数组:
for (var item of Array.from(fruitSet.values())) {
console.log(item);
}
关于javascript - Typescript 循环遍历一组值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48886500/