How to iterate over a Set in TypeScript?

后端 未结 5 1911
逝去的感伤
逝去的感伤 2021-01-03 19:36

How do you iterate over a set in TypeScript? for..of does not work:

\'Set\' is not an array type or a string type

.for

5条回答
  •  日久生厌
    2021-01-03 20:26

    @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 Array.from(set.values())) {
    }
    

    or something like that, which is just terrible.

提交回复
热议问题