Last active
September 15, 2017 18:58
-
-
Save sean-codes/91100e21c0b552c0120f4eba985b5f07 to your computer and use it in GitHub Desktop.
Javascript Split Array into Chunks
This file contains hidden or 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
function splitArrayIntoChunks(arr, chunkLen){ | |
var chunkList = [] | |
var chunkCount = Math.ceil(arr.length/chunkLen) | |
for(var i = 0; i < chunkCount; i++){ | |
chunkList.push(arr.splice(0, chunkLen)) | |
} | |
return chunkList | |
} | |
var testArray = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11] | |
var chunks = splitArrayIntoChunks(testArray, 3) | |
console.log(chunks) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment