Skip to content

Instantly share code, notes, and snippets.

@leandrosilva
Last active April 11, 2016 19:35
Show Gist options
  • Select an option

  • Save leandrosilva/ff47bb1ba89ab78990199b2d4905603f to your computer and use it in GitHub Desktop.

Select an option

Save leandrosilva/ff47bb1ba89ab78990199b2d4905603f to your computer and use it in GitHub Desktop.
Split a JavaScript Array into chuncks
Object.defineProperty(Array.prototype, 'chunk', {
value: function(chunkSize) {
var R = [];
for (var i=0; i<this.length; i+=chunkSize)
R.push(this.slice(i,i+chunkSize));
return R;
}
});
Array.range(10).chunk(3);
// output: [[1,2,3],[4,5,6],[7,8,9],[10]]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment