Created
March 27, 2018 17:35
-
-
Save tidusx18/52542fbebf8c8b6f0fcb8cb8c0ed4732 to your computer and use it in GitHub Desktop.
Split up an array into groups of n values.
This file contains hidden or 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
function partitionArray(arr, n) { | |
const res = []; | |
for (let i = 0; i < Math.floor(arr.length / n); i++) { | |
res.push(arr.slice(i * n, (i + 1) * n)); | |
} | |
res.push(arr.slice(Math.floor(arr.length / n) * n)); | |
return res; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment