我有一个具有以下结构的嵌套对象的对象,如何动态newData
向cost3
数组中添加新项目()?
我已经尝试过,但是没有推送新数据,我在做什么错?
const [file, setFile] = useState({})
setFile(file=> ({
...file,
[cost3]: {
...file.cost3,
newData
}
}))
File
目的:
{
"info": {
},
"client": {
},
"costs": {
"cost1": 1,
"cost2": 5,
"cost3": [
{
"a": "test",
"b": "test",
"c": "test",
},
{
"d": "test",
"e": "test",
"f": "test",
},
//etc..
],
"cost4": [
{
"l": "test",
"n": "test",
"m": "test",
},
//etc..
]
}
}
const file = {
"info": {
},
"client": {
},
"costs": {
"cost1": 1,
"cost2": 5,
"cost3": [{
"a": "test",
"b": "test",
"c": "test",
},
{
"d": "test",
"e": "test",
"f": "test",
},
//etc..
],
"cost4": [{
"l": "test",
"n": "test",
"m": "test",
},
//etc..
]
}
}
const newData = { x: 'I am new' }
console.log(
{
...file,
costs: {
...file.costs,
cost3: [
...file.costs.cost3,
newData
]
}
}
)
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句