This seems simple but I can't get it to work. What I'm trying to do is get items from an array and distribute those one by one into a nested array. Here is my code:
const numberOfSlots = 5;
const numbers = [1, 2, 3, 4, 5, 6, 7, 8]
const nestedArray = Array(numberOfSlots).fill([])
_.forEach(numbers, (number, index) => {
console.log('index', index);
nestedArray[index % numberOfSlots].push(number)
console.log('nested Array', nestedArray)
What I expected to see was something like this:
iteration 1: `[[1][][][][]`
iteration 2: `[[1][2][][][]`
iteration 3: `[[1][2][3][][]`
iteration 4: `[[1][2][3][4][]`
iteration 5: `[[1][2][3][4][5]`
iteration 6: `[[1, 6][2][3][4][5]`
iteration 7: `[[1, 6][2, 7][3][4][5]`
iteration 8: `[[1, 6][2, 7][3, 8][4][5]`
but what I get is this:
Why is this happening? As best I can tell it adds every item to every sub-array on each pass instead of only the array index I'm trying to push it to. How can I fix it?
