I'm looking for a way to stop iterations of underscore.js _.each()
method, but can't find the solution. jQuery .each()
can break if you do return false
.
Is there a way to stop underscore each()?
_([1,2,3]).each(function(v){
if (v==2) return /*what?*/;
})
worked in my case
Maybe you want Underscore's any() or find(), which will stop processing when a condition is met.
I believe if your array was actually an object you could return using an empty object.
Update:
_.find would be better as it breaks out of the loop when the element is found:
** Old **
If you want to conditionally break out of a loop, use _.filter api instead of _.each. Here is a code snippet