Created
April 20, 2017 05:33
-
-
Save Lokua/04a4c09870301020ce0d2af023c28f0d to your computer and use it in GitHub Desktop.
chunk
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
function chunk(array, chunkSize) { | |
if (!chunkSize) throw new Error('chunkSize must be greater than 0') | |
return array.reduce((chunked, value, i) => { | |
if (!(i % chunkSize)) chunked.push([]) | |
chunked[chunked.length - 1].push(value) | |
return chunked | |
}, []) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment