Last active
June 21, 2020 00:36
-
-
Save midnite81/b81274e84a821eefc5a4839a0f8ab231 to your computer and use it in GitHub Desktop.
Javascript - split array into chunks
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
var myArray = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19, 20, 21]; | |
function splitArray(array, numberOfChunks) { | |
arrayCount = array.length; | |
split = array.length / numberOfChunks; | |
chunks = []; | |
for(j = 0; j < numberOfChunks; j++) { | |
var chunk = []; | |
for(i = 0; i < split; i++) { | |
if (typeof array[i] !== 'undefined') { | |
chunk.push(array[i]); | |
} | |
} | |
array.splice(0, chunk.length); | |
chunks.push(chunk); | |
} | |
return chunks; | |
} | |
console.log(splitArray(myArray, 2)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment