javascript split large array into smaller arrays code example
Example 1: 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)
Example 2: array chunk javascript
let input = [1,2,3,4,5,6,7,8];
let chunked = []
let size = 2;
Array.from({length: Math.ceil(input.length / size)}, (val, i) => {
chunked.push(input.slice(i * size, i * size + size))
})
console.log(chunked);