Skip to content

Instantly share code, notes, and snippets.

@tidusx18
Created March 27, 2018 17:35
Show Gist options
  • Save tidusx18/52542fbebf8c8b6f0fcb8cb8c0ed4732 to your computer and use it in GitHub Desktop.
Save tidusx18/52542fbebf8c8b6f0fcb8cb8c0ed4732 to your computer and use it in GitHub Desktop.
Split up an array into groups of n values.
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