我有一个对象:
const data1 = {
connections: [
{
id: 'abfd6e01',
status: 'active',
created: '2023-05-10T11:30:25.0000000Z',
},
],
description: 'Mocked description',
id: '0e8eabf6',
name: 'Mocked Company',
};
我需要做的是克隆状态值从'active'变为'passive'的对象,像这样:
const data1 = {
connections: [
{
id: 'abfd6e01',
status: 'passive',
created: '2023-05-10T11:30:25.0000000Z',
},
],
description: 'Mocked description',
id: '0e8eabf6',
name: 'Mocked Company',
};
我的解决方案是:
const data2 = {...data1, connections:[{...data1.connections, status: 'passive'}]}
不幸的是,它所做的是在连接数组中添加“0”键和额外的“状态”键,所以结果是:
{
connections: [
{
"0": {
id: "abfd6e01",
status: "active",
created: "2023-05-10T11:30:25.0000000Z"
},
status: "passive"
}
],
description: "Mocked description",
id: "0e8eabf6",
name: "Mocked Company"
}