Created
March 24, 2019 06:00
-
-
Save Xetera/834aac650c36811271067756f23ba963 to your computer and use it in GitHub Desktop.
Method that chunks an array of n elements into sub-arrays of max y elements
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 chunk = (array, num) => array.reduce((array, current) => { | |
const last = array[array.length - 1]; | |
const init = array.slice(0, -1); | |
if (!last) { | |
return [...init, [current]] | |
} | |
if (last.length >= num) { | |
return [...array, [current]] | |
} | |
return [...init, [...last, current]]; | |
}, []); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment