Question about Arrays in JavaScript

So I have this problem that I don’t get. I make an array, then I make a second array that copies the first array but in reverse, and I make a third array that copies the second array. I change an element in the second array, but then when I console.log() every array to see the results, all of the arrays changed.

let arr = [["a",1],["b",2],["c",3],["d",4]];
let reverseArr = [...arr].reverse();
let tempReverseArr = [...reverseArr];
// let tempReverseArr = new Array([...reverseArr]);

reverseArr[0][1] = reverseArr[0][1] - 1;

console.log("reverseArr result: ",reverseArr);
console.log("");
console.log("tempReverseArr result: ",tempReverseArr);
console.log("");
console.log("arr result: ", arr);

Is there a way to make an array with the same elements of another array but not let it be affected by changes of the original array that was copied from?

That is because of Shallow Copy

You can read this blog on it

1 Like

Wow, making a deep copy is a bit complex than I feel it should be.