user1019182 user1019182 - 16 days ago 74
TypeScript Question

How to iterate over set in typescript

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 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 {}.

Answer Source

@SnareChops was mostly correct:

mySet.forEach(function(item){
    // do something with "this"
}, **this**);

This works.

I'm guessing:

for(item of mySet.values()){
}

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 ¯_(ツ)_/¯

The only other thing that worked was:

for (var item of Arrays.from(set.values())) {
}

or something like that, which is just terrible.