首页 > 解决方案 > 如何在 TypeScript 中打破 ForEach 循环

问题描述

我有下面的代码,在某些情况下我无法打破循环。

 isVoteTally(): boolean {


 let count = false;
    this.tab.committee.ratings.forEach(element => {

      const _fo = this.isEmptyOrNull(element.ratings.finalOutcome.finaloutlook);
      const _foreign = this.isEmptyOrNull(element.ratings.finalOutcome.foreign);
      const _local = this.isEmptyOrNull(element.ratings.finalOutcome.local);
      const _tally = element.ratings.finalOutcome.voteTally.maj + element.ratings.finalOutcome.voteTally.dis;

      if (_fo == false && _foreign == false && _local == false) {
        if (_tally > 0) {
          **return count = false;**
        }
      } else {
        if (_tally < 0) {
          **return count = false;**
        }
      }
    });
    return count;
  }

在星标区域,我想破解代码并返回布尔值,但我现在无法做到。有谁能够帮助我。

提前致谢。

标签: javascriptangulartypescriptforeachbreak

解决方案


this.tab.committee.ratings.forEach 不是运营商。打字稿允许更多可读的代码。

在样式中使用for循环,如下所示:

for(let a of this.tab.committee.ratings) {
   if(something_wrong) break;
}

ps 忘记 Angular 中的“使用 jQuery 编码”。它只是行不通。


推荐阅读