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

人盡茶涼 提交于 2019-12-24 11:16:52

问题


I have a for loop to get an ID from the DB:

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

Now I get the following TSLint-Error:

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

I'm not sure how to use it in this instance, can anyone help?


回答1:


TSLint see that you could use for-of instead of for-loop it's just enhanced and more cleaner

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

But you can use some method on array objects

 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



来源:https://stackoverflow.com/questions/51897497/tslint-error-expected-a-for-of-loop-instead-of-a-for-loop-with-this-simple

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!