Back
I have an angular foreach loop and i want to break from the loop if I match a value. The following code does not work.
angular.forEach([0,1,2], function(count){ if(count == 1){ break; } });
angular.forEach([0,1,2], function(count){
if(count == 1){
break;
}
});
How can I get this?
To break from the loop if you match a value you can use a boolean to just not going into the body of the loop. As follows:-
var keepGoing = true; angular.forEach([0,1,2], function(count){ if(keepGoing) { if(count == 1){ keepGoing = false; } } });
var keepGoing = true;
if(keepGoing) {
keepGoing = false;
31k questions
32.8k answers
501 comments
693 users