Skip to content

Instantly share code, notes, and snippets.

@Lokua
Created April 20, 2017 05:33
Show Gist options
  • Save Lokua/04a4c09870301020ce0d2af023c28f0d to your computer and use it in GitHub Desktop.
Save Lokua/04a4c09870301020ce0d2af023c28f0d to your computer and use it in GitHub Desktop.
chunk
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