Skip to content

Instantly share code, notes, and snippets.

@nightink
Last active January 20, 2017 06:19
Show Gist options
  • Save nightink/7135101 to your computer and use it in GitHub Desktop.
Save nightink/7135101 to your computer and use it in GitHub Desktop.
underscore each Simulation for loop 'continue' and 'break' keyword
/**
* underscore each Simulation for loop 'continue' and 'break' keyword
*/
var datas = [1, 2, 3, 4, 5, 6, 7];
_.each(datas, function(data, k) {
console.log(data, k);
if(k === 2) {
console.log('continue');
return;
}
if(k > 4) {
console.log('break');
return {};
}
console.log('test');
});
@ashking
Copy link

ashking commented Jan 20, 2017

Interesting. However, IMO it's even simpler with _.every.
returning false will break the loop and returning true will continue the loop.

every_.every(list, [predicate], [context]) Alias: all
Returns true if all of the values in the list pass the predicate truth test. Short-circuits and stops traversing the list if a false element is found.
(http://underscorejs.org/#every)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment