I'm studying JavaScript about array assignment and cloning. I've tried to use spread operator "..." to clone the array A's elements to array B. However, why the Array A's element also changes when I modify B's elements ?
For example :
A = [{id:1, value:3},{id:2, value:1}];
B = [...A];
B[0].value = 4;
console.log(A[0].value);//the output changes to 4, not 3
I found that when cloning normal array, it won't happen this issue:
A = [3, 1];
B = [...A];
B[0] = 4;
console.log(A[0]);//the output is still 3
I know that in JavaScript, one of the purpose of cloning is to avoid two variables reference to the same memory, but why element in A would also be modified in the example?
If I misunderstand any concepts, hope you can help me to figure it out, or if there are any similar issues that were asked and solved before, it would be grateful that you can share the link.