Search code examples
angularjsjavascript-framework

Angular JS break ForEach


I have an angular foreach loop and i want to break from loop if i match a value. The following code does not work.

angular.forEach([0,1,2], function(count){
  if(count == 1){
    break;
  }
});

How can i get this?


Solution

  • There's no way to do this. See https://github.com/angular/angular.js/issues/263. Depending on what you're doing you can use a boolean to just not going into the body of the loop. Something like:

    var keepGoing = true;
    angular.forEach([0,1,2], function(count){
      if(keepGoing) {
        if(count == 1){
          keepGoing = false;
        }
      }
    });