Seemed as a simple task, but doesn't work. I have an array of known length (say 9) and a chunk size (say 3). Array is always dividable by that chunk, no need to test. At first tried:
const chunked = [];
const arr = [1, 2, 3, 4, 5, 6, 7, 8, 9];
for(let i=0; i<3; i++) {
chunked[i] = arr.slice(i*3, 3);
}
console.log(chunked);
But then realised that slice()
probably overwrites its input, so:
const chunked = [];
const arr = [1, 2, 3, 4, 5, 6, 7, 8, 9];
for(let i=0; i<3; i++) {
let temp = arr.slice();
chunked[i] = temp.slice(i*3, 3);
}
console.log(chunked);
But still only first chunk is being created... Where is my error?