Created
September 2, 2019 12:54
-
-
Save felipe-negri/667d13c42d988c9a738bd584e3764de1 to your computer and use it in GitHub Desktop.
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
/** | |
* Returns an array with arrays of the given size. | |
* | |
* @param myArray {Array} Array to split | |
* @param chunkSize {Integer} Size of every group | |
*/ | |
function chunkArray(myArray, chunk_size){ | |
var results = []; | |
while (myArray.length) { | |
results.push(myArray.splice(0, chunk_size)); | |
} | |
return results; | |
} | |
// Split in group of 3 items | |
var result = chunkArray([1,2,3,4,5,6,7,8,9,10,11,12,13,14,15], 10); | |
// Outputs : [ [1,2,3] , [4,5,6] ,[7,8] ] | |
console.log(result); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment