Skip to content

Instantly share code, notes, and snippets.

@Pamblam
Last active February 1, 2018 13:23
Show Gist options
  • Select an option

  • Save Pamblam/cd7c0064e958344a43c463848d556ecd to your computer and use it in GitHub Desktop.

Select an option

Save Pamblam/cd7c0064e958344a43c463848d556ecd to your computer and use it in GitHub Desktop.
Break an array into chunks of a certain size
/**
* Break an array into chunks of a certain size
* @param {array} array - The array to break up
* @param {integer} chunksSize - The size of each subarray
* @returns {Array} - Array of subarrays
*/
function arrayChunk(array, chunksSize){
var i, j, chunk = chunksSize, chunks = [];
for (i = 0, j = array.length; i < j; i += chunk) {
chunks.push(array.slice(i, i + chunk));
}
return chunks;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment