Created
February 2, 2018 10:51
-
-
Save YuriBrunetto/49c64ab1b2a49957a67375a15ff53940 to your computer and use it in GitHub Desktop.
Array into chunks
This file contains 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
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