gpt4 book ai didi

javascript - 扩展运算符在对象的数组内分配数据

转载 作者:行者123 更新时间:2023-11-29 20:47:09 27 4
gpt4 key购买 nike

我需要使用扩展运算符更新以下状态中的数据。必须以 data[0] 应更新为 "vehOn":"Finance"

的方式进行
let state = {
"data": [{
"year": "2017",
"make": "ALFA ROMEO",
"model": "ILX 4D 2.0 PREMIUM PACKAGE"
}],
"error": ""
};

修改后的状态应该是这样的:

let modifiedstate = {
"data": [{
"year": "2017",
"make": "ALFA ROMEO",
"model": "ILX 4D 2.0 PREMIUM PACKAGE",
"vehOn": "Finance"
}],
"error": ""
};

最佳答案

根据 documentation我能看到实现您的结果的唯一方法是:

let state = {
"data": [{
"year": "2017",
"make": "ALFA ROMEO",
"model": "ILX 4D 2.0 PREMIUM PACKAGE"
}],
"error": ""
};

let modifiedstate = { "data": [{ ...state.data[0], ...{vehOn: "Finance"} }],
"error": ""};

console.log(modifiedstate);

关于javascript - 扩展运算符在对象的数组内分配数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53960723/

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