Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Splitting array into two dimensional array based on size parameter
// 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) {
var newArr = [];
beginSlice = 0,
endSlice = size;
for (var i = 0; i < (arr.length/size); i++) {
newArr.push(arr.join("").slice(beginSlice, endSlice).split());
beginSlice += size;
endSlice += size;
}
return newArr;
}
chunkArrayInGroups(["a", "b", "c", "d", "e", "f", "g"], 2)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.