Created
August 22, 2021 18:41
-
-
Save halivert/803863e7ad3679c3713b7f24bd1a767e to your computer and use it in GitHub Desktop.
Split in groups a JS array
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 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