Skip to content

Instantly share code, notes, and snippets.

@jgresalfi
Created September 15, 2016 03:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jgresalfi/79d9ef3f64b68891fc90fce0fcaf4340 to your computer and use it in GitHub Desktop.
Save jgresalfi/79d9ef3f64b68891fc90fce0fcaf4340 to your computer and use it in GitHub Desktop.
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