TSLint错误“使用此简单迭代预期了'for-of'循环而不是'for'循环". [英] TSLint Error "Expected a 'for-of' loop instead of a 'for' loop with this simple iteration"

查看:2477
本文介绍了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-of'循环而不是'for'循环".的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆