TSLint 错误 "Expected a 'for-of' loop instead of a 'for' loop with this simple iteration"

TSLint Error "Expected a 'for-of' loop instead of a 'for' loop with this simple iteration"

我有一个用于从数据库获取 ID 的 for 循环:

for(var i = 0; i < data.GetContractId.length; i++) {
    if (data.GetContractId[i].ContractId) {
        this.contractExists = true;
    }
}

现在我得到以下 TSLint 错误:

Expected a 'for-of' loop instead of a 'for' loop with this simple iteration

我不确定在这种情况下如何使用它,有人可以帮忙吗?

TSLint 看到你可以使用 for-of 而不是 for-loop 它只是增强了并且更干净

for (let contract of data.GetContractId) {
  if (contract.ContractId) {
    this.contractExists = true;
    break;
  }
}

但是你可以在数组对象上使用some方法

 this.contractExists  = data.GetContractId.some(contract => contract.ContractId);

The some() method tests whether at least one element in the array passes the test implemented by the provided function.

some