Skip to content

Instantly share code, notes, and snippets.

@raykao
Created August 20, 2019 02:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save raykao/8ec4d17099aabbe5b967d28703ec784a to your computer and use it in GitHub Desktop.
Save raykao/8ec4d17099aabbe5b967d28703ec784a to your computer and use it in GitHub Desktop.
Turn an array into an array of arrays...to batch process some data.
function batcherize(batchSize, unbatchedData) {
const batchedData = [];
const batchNumber = Math.ceil(unbatchedData.length/batchSize)
for (let batchIndex = 0; batchIndex < batchNumber; batchIndex++) {
const currentBatchPosition = (batchIndex * batchSize);
const currentBatchOffset = currentBatchPosition + (batchSize);
const currentBatchData = unbatchedData.slice(currentBatchPosition, currentBatchOffset);
batchedData.push(currentBatchData);
}
return batchedData;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment