简体   繁体   English

如何将新对象添加到嵌套在 ReactJS 对象数组中的对象数组?

[英]How to add a new object to an array of objects that is nested inside an array of objects in ReactJS?

I am new to ReactJS and JS and always get stuck with updating the state using object spread.我是 ReactJS 和 JS 的新手,总是被困在使用对象传播更新状态。

I have simplified my data as follows and consider the 0th index for the state for my example,我已将我的数据简化如下,并在我的示例中考虑状态的第 0 个索引,

state = [
  {
    id: "1",
    name: "Some name",
    someNestedArrayProperty: [
      {
        id: "1",
        name: "Name 1"
      },
      {
        id: "2",
        name: "Name 2"
      }
    ]
  },
 {...}
]

I want to add a newObject into someNestedArrayProperty that I receive from action creator, say我想将一个 newObject 添加到我从动作创建者那里收到的 someNestedArrayProperty 中,例如

 newObject = {
        id: "3",
        name: "Name 3"
      }

I am not getting the correct syntax.我没有得到正确的语法。 I tried the following and it is messing up the state.我尝试了以下操作,但它弄乱了状态。 Below is the simplified code of what I am trying to do in my application,以下是我在应用程序中尝试执行的操作的简化代码,

  let someNestedArrayProperty= [
    ...state[0].someNestedArrayProperty,
    action.someNestedArrayProperty
  ];
  return [
    {
      ...state,
      [0]: {  //I guess the problem is in this line "[0]", but I am not getting the correct syntax
        ...state[0],
        someNestedArrayProperty: someNestedArrayProperty
      }
    }
  ];

Note: I am returning the modified state from the Redux reducer注意:我从 Redux reducer 返回修改后的状态

You can use map function to modify your state like following:您可以使用 map 函数来修改您的状态,如下所示:

const newState = state.map((s, idx) => {

    // Modification needed elemet
    if (idx === 0) {
        return {
            ...s,
            someNestedArrayProperty: someNestedArrayProperty
        }
    }
    // Modification not needed
    else {
        return s;
    }

})

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何更新嵌套在 ReactJS 对象中的数组的属性 - How to update properties of an array nested inside objects in ReactJS 如何将多个对象添加到对象内的数组中 - How to add multiple Objects into an Array inside an Object 如何将新的 object 添加到 localStorage 中的对象数组? - How to add a new object to an array of objects in localStorage? 用新的对象数组替换对象数组中的嵌套数组 - Replacing a nested array inside an array of objects with new array of objects 想要在嵌套对象内添加对象数组:Javascript - Want to add an array of objects inside a nested object: Javascript 基于对象内部数组的新对象数组? - New array of objects based on an array inside an object? 如何向对象添加新的对象数组以使其嵌套,如果新添加的数组已存在,则向该数组添加另一个对象? - How to add a new array of objects to an object to make it nested and if newly added array already exists, add another object to that array? 渲染嵌套数据(来自另一个数组的 object 中的数组的对象)来自后端 - ReactJS (JSX) - Rendering nested data (objects from an array inside an object of another array ) From backend - ReactJS (JSX) 如何使用node.js在mongodb的嵌套数组内添加新对象? - How to add new objects inside nested array for mongodb using node.js? 如何将 object 添加到另一个对象数组中的数组? - How to add object to an array inside another array of objects?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM