Last active
April 11, 2016 19:35
-
-
Save leandrosilva/ff47bb1ba89ab78990199b2d4905603f to your computer and use it in GitHub Desktop.
Split a JavaScript Array into chuncks
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
| 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