javascript convert array value to fixed number of chunks code example
Example 1: react split array into chunks
const chunkSize = 10;
const arr = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17];
const groups = arr.map((e, i) => {
return i % chunkSize === 0 ? arr.slice(i, i + chunkSize) : null;
}).filter(e => { return e; });
console.log({arr, groups})
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);