Last active
January 20, 2017 06:19
-
-
Save nightink/7135101 to your computer and use it in GitHub Desktop.
underscore each Simulation for loop 'continue' and 'break' keyword
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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'); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Interesting. However, IMO it's even simpler with _.every.
returning
false
will break the loop and returningtrue
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)