let's look at the code:
Test() {
let array1 = new Array(5).fill({ a: 0 })
let array2 = new Array(5).fill({ a: 0 })
for (let i = 0; i < 5; i++) {
setTimeout(() => {
array1[i].a = i
array2[i] = {a:i}
console.warn("array = ", array1)
console.warn("array2 = ", array2)
}, 0.2 * i)
}
}
In this case, I wanna assign a series of values to the array1 & array2, and there are two ways to do it, which lead to totally different results.
In the case array1[i].a = i
, after all the code is ran, the result is array = [{a:4},{a:4},{a:4},{a:4},{a:4}]
, which is not what i wanted.
In the second case array2[i] = {a:i}
, the result will be [{a:0},{a:1},{a:2},{a:3},{a:4}]
as expected.
I wanna know why is it like this? What's the machanics behind this phenomenon?
Thank you.