Skip to content

Instantly share code, notes, and snippets.

@felipe-negri
Created September 2, 2019 12:54
Show Gist options
  • Save felipe-negri/667d13c42d988c9a738bd584e3764de1 to your computer and use it in GitHub Desktop.
Save felipe-negri/667d13c42d988c9a738bd584e3764de1 to your computer and use it in GitHub Desktop.
/**
* 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