bobdolan
bobdolan

Reputation: 573

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

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?

Upvotes: 2

Views: 2623

Answers (1)

Muhammed Albarmavi
Muhammed Albarmavi

Reputation: 24424

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

Upvotes: 1

Related Questions