I am facing an issue with manipulating child arrays within a parent array. Specifically, I want to manipulate the first element in the first child array without affecting the rest of the arrays. However, my current code is shifting the first elements of all child arrays instead of just the first one.
After some investigation, I suspect that the problem lies in the fact that fillWith is being deep-copied. I have tried various methods to make it a shallow copy but haven't been successful.
What am I overlooking?
let arr= Array(3)
let fillWith = Array.from({length: 5}, (_, i) => i+1);
arr.fill([...[], ...fillWith]) // Try 1
//arr.fill(fillWith.slice()) // Try 2
console.log(arr) // -> [[1, 2, 3, 4, 5], [1, 2, 3, 4, 5], [1, 2, 3, 4, 5]]
arr[0].shift();
console.log(arr)
/*
How I want it to be -> [[2, 3, 4, 5], [1, 2, 3, 4, 5], [1, 2, 3, 4, 5]]
What it actually becomes -> [[2, 3, 4, 5], [2, 3, 4, 5], [2, 3, 4, 5]]
*/