将对象压入对象数组,导致整个数组发生意外更改



让我们直奔主题

// lets say i have a simple array of objects inside an object
const abc = {
a: 1,
b: [{aa: 1, bb: 2}, {aa: 2, bb: 2}]
}
// then I want to update the array with push a new object
abc.b.push({aa: 3, bb: 3})

但是为什么会产生

{
a: 1,
b: [[Object], [Object], [Object]]
}

我期待结果

{
a: 1,
b: [{aa: 1, bb: 2}, {aa: 2, bb: 2}, {aa: 3, bb: 3}]
}

结果我得到nodejsconsole.log(abc)

const abc = {
a: 1,
b: [{aa: 1, bb: 2}, {aa: 2, bb: 2}]
}
// then I want to update the array with push a new object
abc.b.push({aa: 3, bb: 3})
console.log(JSON.stringify(abc))

最新更新