Created
September 15, 2016 03:05
-
-
Save jgresalfi/79d9ef3f64b68891fc90fce0fcaf4340 to your computer and use it in GitHub Desktop.
Splitting array into two dimensional array based on size parameter
This file contains 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
// 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