Skip to content

Instantly share code, notes, and snippets.

@YuriBrunetto
Created February 2, 2018 10:51
Show Gist options
  • Save YuriBrunetto/49c64ab1b2a49957a67375a15ff53940 to your computer and use it in GitHub Desktop.
Save YuriBrunetto/49c64ab1b2a49957a67375a15ff53940 to your computer and use it in GitHub Desktop.
Array into chunks
const myArray = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13]
const chunkSize = 4
const chunks = myArray.reduce((ar, it, i) => {
const ix = Math.floor(i / chunkSize)
if (!ar[ix]) ar[ix] = []
ar[ix].push(it)
return ar
}, [])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment