Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to break ForEach Loop in TypeScript

I have a the below code, on which i am unable to break the loop on certain conditions.

 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;
  }

On the star-marked area, i want to break the code and return the boolean value, but i am unable to do it right now. Can anybody help me.

Thanks in advance.

like image 954
Arka Avatar asked Aug 08 '18 12:08

Arka


People also ask

How do I terminate a forEach loop in TypeScript?

To break a forEach() loop in TypeScript, throw and catch an error by wrapping the call to the forEach() method in a try/catch block. When the error is thrown, the forEach() method will stop iterating over the collection.

How do you break a forEach loop?

There is no way to stop or break a forEach() loop other than by throwing an exception. If you need such behavior, the forEach() method is the wrong tool. return; // would this make a difference? no.

How do you break out of a loop in TypeScript?

Using TypeScript break to terminate a loopThe break statement allows you to terminate a loop and pass the program control over the next statement after the loop. You can use the break statement inside the for , while , and do... while statement.

Can we use break in forEach JavaScript?

Officially, there is no proper way to break out of a forEach loop in javascript. Using the familiar break syntax will throw an error. If breaking the loop is something you really need, it would be best to consider using a traditional loop.


3 Answers

this.tab.committee.ratings.forEach is not an operator.

Typescript allows for much more readable code.

Use a for loop in style as follows:

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

p.s. forget "coding as with jQuery" in Angular. It just doesn't work.

like image 184
Roberc Avatar answered Oct 17 '22 02:10

Roberc


It is not possible to break from forEach() normally.

Alternatively you can use Array.every() because you wish to return false while breaking the loop.

If you want to return true, then you can use Array.some()

this.tab.committee.ratings.every(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;**
    }
  }
});
like image 28
Amit Chigadani Avatar answered Oct 17 '22 02:10

Amit Chigadani


You cannot ‘break’, it won’t even run because the break instruction is not technically in a loop. Solution? Just use a normal for loop. Nobody would laugh at you.

like image 7
Skraloupak Avatar answered Oct 17 '22 03:10

Skraloupak