gpt4 book ai didi

javascript - 如何使用扩展运算符更改对象的嵌套属性?

转载 作者:行者123 更新时间:2023-12-02 07:59:45 24 4
gpt4 key购买 nike

这是我的情况的一个干净版本:

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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com