I was trying to get the x
and y
value from obj.coords
, but the value is not updated using destructuring assignment.
let obj = { name: 'test', coords: [0, 0] }
let x = 0, y = 0
obj = { ...obj, coords: [12, 16] }
// not working
[x, y] = obj.coords
console.log('coords:', x, y)
I tried assign them one by one, it works:
let obj = { name: 'test', coords: [0, 0] }
let x = 0, y = 0
obj = { ...obj, coords: [12, 16] }
// works
x = obj.coords[0]
y = obj.coords[1]
console.log('coords:', x, y)
Is there a reason for that? Or is it some kind of bug?