我有一个for循环,用于从数据库中获取ID:
for(var i = 0; i < data.GetContractId.length; i++) { if (data.GetContractId[i].ContractId) { this.contractExists = true; } }现在,我收到以下TSLint错误:
通过此简单的迭代,预计使用"for-of"循环,而不是"for"循环
我不确定在这种情况下如何使用它,任何人都可以帮忙吗?
解决方案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);
some()方法测试数组中是否至少有一个元素 通过了由提供的功能实现的测试.
一些
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?
解决方案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
更多推荐
TSLint错误“使用此简单迭代预期了'for
发布评论