Skip to content

Instantly share code, notes, and snippets.

@halivert
Created August 22, 2021 18:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save halivert/803863e7ad3679c3713b7f24bd1a767e to your computer and use it in GitHub Desktop.
Save halivert/803863e7ad3679c3713b7f24bd1a767e to your computer and use it in GitHub Desktop.
Split in groups a JS array
const splitIn = (elements, groups) => {
return elements.reduce((result, val, _, elements) => {
let lastItem = result[result.length - 1];
if (lastItem.length === Math.ceil(elements.length / groups)) result.push([val]);
else lastItem.push(val);
return result;
}, [[]]);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment