The _.each function accepts an array or an object, an iteratee function and an optional context object, the iteratee function is invoked once and in order for each array item The iteratee function provides 3 arguments item - The current iterated object (or value if an object was passed) i - The index of the iterated ...
The _. property() function is used to return a function that will return the specified property of any passed-in object.
Adding Underscore to a Node. Once added, underscore can be referred in any of the Node. js modules using the CommonJS syntax: var _ = require('underscore'); Now we can use the object underscore (_) to operate on objects, arrays and functions.
You can't break from the each
method—it emulates the native forEach
method's behavior, and the native forEach
doesn't provide to escape the loop (other than throwing an exception).
However, all hope is not lost! You can use the Array.every
method. :)
From that link:
every
executes the providedcallback
function once for each element present in the array until it finds one wherecallback
returns a false value. If such an element is found, theevery
method immediately returns false.
In other words, you could do something convoluted like this (link to JSFiddle):
[1, 2, 3, 4].every(function(n) {
alert(n);
return n !== 3;
});
This will alert 1
through 3
, and then "break" out of the loop.
You're using underscore.js, so you'll be pleased to learn that it does provide an every
method—they call it every
, but as that link mentions, they also provide an alias called all
.
Update:
_.find would be better as it breaks out of the loop when the element is found:
var searchArr = [{id:1,text:"foo"},{id:2,text:"bar"}];
var count = 0;
var filteredEl = _.find(searchArr,function(arrEl){
count = count +1;
if(arrEl.id === 1 ){
return arrEl;
}
});
console.log(filteredEl);
//since we are searching the first element in the array, the count will be one
console.log(count);
//output: filteredEl : {id:1,text:"foo"} , count: 1
** Old **
If you want to conditionally break out of a loop, use _.filter api instead of _.each. Here is a code snippet
var searchArr = [{id:1,text:"foo"},{id:2,text:"bar"}];
var filteredEl = _.filter(searchArr,function(arrEl){
if(arrEl.id === 1 ){
return arrEl;
}
});
console.log(filteredEl);
//output: {id:1,text:"foo"}
You can have a look to _.some
instead of _.each
.
_.some
stops traversing the list once a predicate is true.
Result(s) can be stored in an external variable.
_.some([1, 2, 3], function(v) {
if (v == 2) return true;
})
See http://underscorejs.org/#some
_([1,2,3]).find(function(v){
return v if (v==2);
})
Maybe you want Underscore's any() or find(), which will stop processing when a condition is met.
Like the other answers, it's impossible. Here is the comment about breaker in underscore underscore issue #21
You cannot break a forEach
in underscore, as it emulates EcmaScript 5 native behaviour.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With