Created
August 20, 2019 02:06
-
-
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.
This file contains 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 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