TSLint错误“使用此简单迭代预期了'for

编程入门 行业动态 更新时间:2024-10-08 22:19:38
本文介绍了TSLint错误“使用此简单迭代预期了'for-of'循环而不是'for'循环".的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有一个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

本文发布于:2023-11-29 15:47:44,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1646844.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:错误   迭代   简单   TSLint

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!