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?
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?
As the other answers state, Angular doesn't provide this functionality. jQuery does however, and if you have loaded jQuery as well as Angular, you can use
See http://api.jquery.com/jquery.each/
please use some or every instances of ForEach,
Example for some:
Example for every:
Find more information
http://www.jsnoob.com/2013/11/26/how-to-break-the-foreach/
Normally there is no way to break an "each" loop in javascript. What can be done usually is to use "short circuit" method.
The
angular.forEach
loop can't break on a condition match.My personal advice is to use a NATIVE FOR loop instead of
angular.forEach
.The NATIVE FOR loop is around 90% faster then other for loops.
USE FOR loop IN ANGULAR:
Concretely, you can exit of a
forEach
loop, and of any place, throw an exception.However, it is better if you use other library or implement your own function, a
find
function in this case, so your code is most high-level.