so I just came across this, but could anyone explain it to me, why is this phenomenon present here? I didn't find any help online...
1.So if I were to get the following two arrays:
let a = [];
let b = ['','',''];
2.and then push array b into array a three times:
a.push(b);
a.push(b);
a.push(b);
3.but then when I try to change the first element of the first array like so:
a[0][0] = 'foo';
4.then all the first element of ALL the arrays change, not just the first one, as console.log(a) outputs:
(3) [Array(3), Array(3), Array(3)]
0: Array(3)
0: "foo"
1: ""
2: ""
1: Array(3)
0: "foo"
1: ""
2: ""
2: Array(3)
0: "foo"
1: ""
2: ""
I would like to ask, why does this happen and in what way could I change an element based on its index inside of array a
aare all holding a reference tob-- "they all get updated" is a bit misleading, you're updating one object and it appears in multiple places.