这是我的情况的干净版本:
const person1 = {
name: "Person1 Name",
hairColor: "Brown",
backpack: {
color: "Army-Green",
content: [
"item1",
"item2",
"..."
]
}
}
我正在尝试只更改 背包颜色
我已经尝试过下面的代码但没有成功:
person = {...person1, backpack.color: "New backpack color"}};
person = {...person1, backpack: {...backpack, color: "New backpack color"}};
person = {...person1, backpack: {color: "New backpack color"}};
person = {...person1, backpack = {...backpack, color: "New backpack color"}};
最佳答案
const person2 = {...person1, backpack: {...person1.backpack, color: 'Red' }}
关于javascript - 如何使用扩展运算符更改对象的嵌套属性?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/58995336/