create batches from array js code example
Example 1: javascript array split chunk
const chunk = (arr, size) => arr.reduce((acc, e, i) => (i % size ? acc[acc.length - 1].push(e) : acc.push([e]), acc), []);
chunk([1, 2, 3, 4, 5, 6, 7, 8], 3);
chunk([1, 2, 3, 4, 5, 6, 7, 8], 4);
Example 2: javascript how to split array into subarrays javascript
let randomArray = [3, 5, 1, 5, 7,];
let arrayOfArrays = [];
function splitArray( array ) {
while (array.length > 0) {
let arrayElement = array.splice(0,1);
arrayOfArrays.push(arrayElement);
}
return arrayOfArrays;
}
splitArray(randomArray)