I'm encountering an issue with updating values in a complex JavaScript object that contains an array. Whenever I attempt to set the value for one attribute at a specific index, it ends up being applied to all items in the array.
Let's illustrate this with a simple example:
const obj = new Object();
obj.arr = [];
obj.arr[0] = {pos:[0,0]};
obj.arr[1] = {pos:[0,0]};
If I try to assign values to attributes using specific indices:
obj.arr[0].pos = [10,10];
obj.arr[1].pos = [5,5];
The problem arises when the value [5,5]
seems to be propagating to both items in the array. This results in:
console.log(obj.arr[0].pos)
returning [5,5]
and
console.log(obj.arr[1].pos)
also yielding [5,5]
Although my actual object is more intricate, this simplified scenario conveys the essence of the issue...
Any suggestions or insights on how to address this?