简体   繁体   English

[tslint] 使用这个简单的迭代(prefer-for-of)期望使用“for-of”循环而不是“for”循环

[英][tslint]Expected a 'for-of' loop instead of a 'for' loop with this simple iteration (prefer-for-of)

I have got a tslint error to my for loop when I try to resolve it it says to convert to for-of.当我尝试解决它时,我的 for 循环有一个 tslint 错误,它说要转换为 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我看过很多文档,但没有帮助。如何解决 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;
    }
}

It is asking you to use format like the following. 它要求您使用如下格式。 The of keyword loops over the objects in the array instead of looping over the indexes of the array. of关键字循环遍历数组中的对象,而不是循环遍历数组的索引。 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 of syntax). 我猜想那是因为你只使用索引来得到数组中的值(可以使用被清理的方式触发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);

In tslint.json we can add inside the rules:tslint.json我们可以在规则中添加:

"rules": {
    "prefer-for-of": false
}

This will resolve the issue by disabling the relevant lint rule.这将通过禁用相关的 lint 规则来解决问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM