Skip to content

Instantly share code, notes, and snippets.

@jperler
Last active December 8, 2017 01:05
Show Gist options
  • Save jperler/5624450 to your computer and use it in GitHub Desktop.
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]]
_.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