Skip to content

Instantly share code, notes, and snippets.

@yanarp
Created July 4, 2022 14:50
Show Gist options
  • Save yanarp/0f7b91645e7f128e64abd1d6e7a12a77 to your computer and use it in GitHub Desktop.
Save yanarp/0f7b91645e7f128e64abd1d6e7a12a77 to your computer and use it in GitHub Desktop.
Fnc to split array in numbered batches
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