英文:
replacing and object inside an existing array of objects
问题
I have an array of objects which is part of a form submission,
const cartsObjForSave=[
{
"id": "full",
"name": "Full Cart",
"weight": 0,
"volume": 0,
"dimensions": {
"length": 0,
"breadth": 0,
"height": 0
},
"quantity": parseInt(state.fullCarts)
},
{
"id": "half",
"name": "Half Cart",
"weight": 0,
"volume": 0,
"dimensions": {
"length": 0,
"breadth": 0,
"height": 0
},
"quantity": parseInt(state.halfCarts)
},
{
"id": "smu",
"name": "SMU",
"weight": 0,
"volume": 0,
"dimensions": {
"length": 0,
"breadth": 0,
"height": 0
},
"quantity": parseInt(state.smu)
}
]
const body=[
{
"id": state.name,
"name": state.name,
carts: cartsObj
},
...carts
]
In the cartsObjForSave
array, the quantity key is taken from the state by setting it in a React useState
variable. The carts
array in the body
array is from the API.
My useState
initial is:
const [state, setState] = useState({name:'', fullCarts: null, halfCarts: null, smu: null});
The carts
object from the API is:
const carts={
"name": "G75-98",
"id": "G75-98",
"carts": [
{
"quantity": 3,
"dimensions": {
"height": 0,
"breadth": 0,
"length": 0
},
"volume": 0,
"weight": 0,
"name": "Full Cart",
"id": "full"
},
{
"quantity": 5,
"dimensions": {
"height": 0,
"breadth": 0,
"length": 0
},
"volume": 0,
"weight": 0,
"name": "Half Cart",
"id": "half"
},
{
"quantity": 1,
"dimensions": {
"height": 0,
"breadth": 0,
"length": 0
},
"volume": 0,
"weight": 0,
"name": "SMU",
"id": "smu"
}
]
}
My aim is to replace the carts
object if the user has edited, say, name or quantity in the carts
object and pass it as a body
for saving.
I tried using the following code to begin with but it doesn't seem to work as expected. Is there a better approach to implement this?
const res = carts.map(c => {
if (c.name === state.name) {
c = body[0];
}
return c;
})
英文:
I have an array of objects which is part of a form submission,
const cartsObjForSave=[
{
"id": "full",
"name": "Full Cart",
"weight": 0,
"volume": 0,
"dimensions": {
"length": 0,
"breadth": 0,
"height": 0
},
"quantity": parseInt(state.fullCarts)
},
{
"id": "half",
"name": "Half Cart",
"weight": 0,
"volume": 0,
"dimensions": {
"length": 0,
"breadth": 0,
"height": 0
},
"quantity": parseInt(state.halfCarts)
},
{
"id": "smu",
"name": "SMU",
"weight": 0,
"volume": 0,
"dimensions": {
"length": 0,
"breadth": 0,
"height": 0
},
"quantity": parseInt(state.smu)
}
]
const body=[
{
"id": state.name,
"name": state.name,
carts: cartsObj
},
...carts
]
in the cartsObjForSave array the quantity key is taken from the state by setting it in a react useState variable the carts array in the body array is from the api
my useState initial is
const [state,setState]=useState({name:'',fullCarts:null,halfCarts:null,smu:null});
carts object from the api is,
const carts={
"name": "G75-98",
"id": "G75-98",
"carts": [
{
"quantity": 3,
"dimensions": {
"height": 0,
"breadth": 0,
"length": 0
},
"volume": 0,
"weight": 0,
"name": "Full Cart",
"id": "full"
},
{
"quantity": 5,
"dimensions": {
"height": 0,
"breadth": 0,
"length": 0
},
"volume": 0,
"weight": 0,
"name": "Half Cart",
"id": "half"
},
{
"quantity": 1,
"dimensions": {
"height": 0,
"breadth": 0,
"length": 0
},
"volume": 0,
"weight": 0,
"name": "SMU",
"id": "smu"
}
]
}
my aim is to replace the carts object if the user has edited say name or quantity in the carts object and pass it as a body for saving
i tried using
const res= carts.map(c=>{if(c.name===state.name){
c=body[0];
}
return c})
to begin with but doesn't seems to work as expected is there a better approach to implement this
答案1
得分: 1
我建议你只有在名称与state.name匹配时才更新(改变)项目条目。
英文:
Since you are OK with mutating the array, I would recommend:
const updatedBody = body.map((item) => {
if (item.name === state.name) {
return {
...item,
carts: cartsObjForSave,
};
}
return item;
});
You only update (mutate) the item entry when its name matches the state.name.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论