Created
July 11, 2013 23:07
-
-
Save DiegoSeC/5980123 to your computer and use it in GitHub Desktop.
Split array in chunks using underscore.js
This file contains 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
# Thanks to: http://stackoverflow.com/questions/8566667/split-javascript-array-in-chunks-using-underscore-js | |
_.mixin | |
chunk: (array, unit) -> | |
result = _.groupBy array, (element, index) -> | |
Math.floor index / unit | |
_.toArray result | |
data = [1,2,3,4,5,6,7,8,9] | |
# We pass the array and the size of the chunk | |
list = _.chunk data, 2 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
AFICS this may not work on all implementations, a JS object has "an unordered collection of properties".