How can we do this using the new
There's no built-in option to
forEach. To interrupt execution we would have to throw an exception of some sort.
This works because
true as soon as any of the callbacks, executed in array order, return true, short-circuiting the execution of the rest.
We can use
For old browser support this below code:
This code does not print the array elements after the number 5:
Consider to use jquery's each method, since it allows to return false inside callback function:
Lodash libraries provides
takeWhile method that can be bound with map/reduce/fold etc:
Here we can use