如何迭代 TypeScript 中的 Set?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/35193471/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to iterate over a Set in TypeScript?
提问by Programmer9000
How do you iterate over a set in TypeScript? for..of does not work:
你如何在 TypeScript 中迭代一个集合?for..of 不起作用:
'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 {}.
.forEach
是不可接受的,因为它隐藏了this
。我不想在 try catch 块中执行 while 循环。我错过了什么?它不可能笨到需要 try {while} catch {}。
回答by Programmer9000
@SnareChops was mostly correct:
@SnareChops 大部分是正确的:
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 ˉ_(ツ)_/ˉ
如果我不使用 es-shim 的东西会工作,这对我来说一切都搞砸了。但是垫片的东西是由Angular 2的工作人员规定的所以ˉ_(ツ)_/ˉ
The only other thing that worked was:
唯一有效的另一件事是:
for (var item of Array.from(set.values())) {
}
or something like that, which is just terrible.
或类似的东西,这太可怕了。
回答by SnareChops
You can still use .forEach
with the correct this
by using a regular function instead of an arrow function
您仍然可以通过使用常规函数而不是箭头函数来使用.forEach
正确this
的
mySet.forEach(function(item){
expect(this).toEqual(item);
});
Compared to
相比
class MyClass{
...
doSomething():{
mySet.forEach((item) => {
expect(this instanceof MyClass).toEqual(true);
});
}
}
Another way to iterate is to use a for loop over the values
另一种迭代方法是对值使用 for 循环
for(item of mySet.values()){
...
}
More information on iterating Set
with foreach can be found here
Set
可以在此处找到有关使用 foreach进行迭代的更多信息
回答by peterh - Reinstate Monica
Extending the most upvoted answer, it is also type-safe if we use let
for the iteration variable, so:
扩展最受好评的答案,如果我们let
用于迭代变量,它也是类型安全的,因此:
for (let elem of setOfElems) {
... do anything with elem...
}
This will guarantee that elem
will have the type X, if setOfElems
was declared as Set<X>
.
这将保证elem
将具有类型 X,如果setOfElems
被声明为Set<X>
.
回答by Jan Clemens Stoffregen
This worked for me:
这对我有用:
this.mySet.forEach((value: string, key: string) => {
console.log(key);
console.log(value);
});
I found it here: Other Stack Overflow question
我在这里找到了:其他堆栈溢出问题
回答by Newbyte
You can use for ... of
in TypeScript if you add "es6"
as "lib"
in your compiler options, and "es6"
as target. You may also use "es2015.iterable"
in place of "es6"
in your lib if that better suits your needs.
for ... of
如果在编译器选项中添加"es6"
as"lib"
和"es6"
作为目标,则可以在 TypeScript中使用。如果这更适合您的需要,您也可以在您的库中使用"es2015.iterable"
代替"es6"
。
For example (this would be your tsconfig.json):
例如(这将是您的 tsconfig.json):
{
"compilerOptions": {
"target": "es6",
"lib": [
"es6",
"dom"
]
},
"exclude": [
"node_modules"
]
}
Related issue on GitHub: https://github.com/Microsoft/TypeScript/issues/12707
GitHub 上的相关问题:https: //github.com/Microsoft/TypeScript/issues/12707