Javascript [tslint] 使用这个简单的迭代(prefer-for-of)期望使用“for-of”循环而不是“for”循环
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/49243048/
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
[tslint]Expected a 'for-of' loop instead of a 'for' loop with this simple iteration (prefer-for-of)
提问by Juke
I have got a tslint error to my for loop when I try to resolve it it says to convert to for-of. I have seen many docs but its not helpful.How can I solve the lint error and I cannot do tslint:disable-next-line:prefer-for-of
当我尝试解决它时,我的 for 循环有一个 tslint 错误,它说要转换为 for-of。我看过很多文档,但没有帮助。如何解决 lint 错误并且我不能执行 tslint:disable-next-line:prefer-for-of
for (let i = 0; i < this.rows.length; ++i) {
if (!this.rows[i].selected) {
this.selectAllChecked = false;
break;
}
}
回答by Daniel W Strimpel
It is asking you to use format like the following. The ofkeyword loops over the objects in the array instead of looping over the indexes of the array. I'm assuming it is triggering because you are only using the index as a way of getting to the value in the array (which can be cleaned up using the ofsyntax).
它要求您使用如下格式。的of在阵列的代替循环在阵列的索引中的对象关键字环路。我假设它正在触发,因为您只是使用索引作为获取数组中值的一种方式(可以使用of语法清理)。
for (let row of this.rows) {
if (!row.selected) {
this.selectAllChecked = false;
break;
}
}
As a note, you can accomplish the same thing using the following one-liner:
请注意,您可以使用以下单行代码完成相同的事情:
this.selectAllChecked = this.rows.every(row => row.selected);

