Skip to content

Instantly share code, notes, and snippets.

@Xetera
Created March 24, 2019 06:00
Show Gist options
  • Save Xetera/834aac650c36811271067756f23ba963 to your computer and use it in GitHub Desktop.
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
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