Created
July 4, 2022 14:50
-
-
Save zepochs/0f7b91645e7f128e64abd1d6e7a12a77 to your computer and use it in GitHub Desktop.
Fnc to split array in numbered batches
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 splitPayload(arr, batchSize) { | |
let tempArr = arr; | |
let result = []; | |
let rest = arr.length % batchSize; // number of non divisble elements | |
let partLength = Math.floor(tempArr.length / batchSize); // number to divide remaining number of array element | |
let restData = tempArr.splice(0, rest) | |
result.push(restData) | |
for (let i = 0; i < partLength; i++) { | |
let restData = arr.splice(0, batchSize) | |
result.push(restData); | |
} | |
return result; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment