替换现有对象数组中的对象。

huangapple go评论104阅读模式
英文:

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.

huangapple
  • 本文由 发表于 2023年5月6日 21:36:35
  • 转载请务必保留本文链接:https://go.coder-hub.com/76189183.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定