-
Notifications
You must be signed in to change notification settings - Fork 0
Chunky Monkey
Islam Ibakaev edited this page Apr 26, 2016
·
1 revision
Write a function that splits an array (first argument) into groups the length of size
(second argument) and returns them as a two-dimensional array.
function chunkArrayInGroups(arr, size) {
return arr.length > size ? [arr.splice(0, size)].concat(chunkArrayInGroups(arr, size)) : [arr];
}
chunkArrayInGroups([0, 1, 2, 3, 4, 5], 3); // returns [[0, 1, 2], [3, 4, 5]]