Last active
December 8, 2017 01:05
-
-
Save jperler/5624450 to your computer and use it in GitHub Desktop.
Array chunking function for Underscore.js. Usage: _.chunks([1,2,3,4,5,6,7], 2) => [[1,2],[2,3],[4,5],[5,6],[7]]
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
_.mixin({ | |
chunks: function(arr, size) { | |
var len = arr.length, | |
chunk_len = Math.ceil(len/size), | |
chunks = []; | |
for (var i = 0; i < chunk_len; i++) { | |
chunks.push(arr.slice(i*size, (i+1)*size)); | |
} | |
return chunks; | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment