Skip to content

Instantly share code, notes, and snippets.

@azimidev
Forked from jperler/chunk.js
Created December 8, 2017 01:05
Show Gist options
  • Save azimidev/f0c6dfa9eaffc517c5cb227ae56ec6e6 to your computer and use it in GitHub Desktop.
Save azimidev/f0c6dfa9eaffc517c5cb227ae56ec6e6 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