如何遍历 TypeScript 中的 Set? [英] How to iterate over a Set in TypeScript?
问题描述
你如何在 TypeScript 中迭代一个集合?for..of 不起作用:
How do you iterate over a set in TypeScript? for..of does not work:
'Set<string>' is not an array type or a string type
.forEach
是不可接受的,因为它隐藏了this
.我不想在 try catch 块中执行 while 循环.我错过了什么?它不可能笨到需要 try {while} catch {}.
.forEach
is not acceptable, because it hides this
. I'd prefer not to do a while loop in a try catch block. What am I missing? It can't it possibly so clumsy as to require try {while} catch {}.
推荐答案
@SnareChops 大部分是正确的:
@SnareChops was mostly correct:
mySet.forEach(function(item){
// do something with "this"
}, **this**);
这有效.
我猜:
for(item of mySet.values()){
}
如果我不使用 es-shim 的东西,它会工作,这对我来说一切都搞砸了.但是垫片的东西是由Angular 2的工作人员规定的所以¯_(ツ)_/¯
Would work if I weren't working with es-shim stuff which is messing everything up for me. But the shim stuff is prescribed by the Angular 2 crew so ¯_(ツ)_/¯
唯一有效的是:
for (var item of Array.from(set.values())) {
}
或类似的东西,这太可怕了.
or something like that, which is just terrible.
这篇关于如何遍历 TypeScript 中的 Set?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!