繁体   English   中英

如何用内部包含多个对象的新数组替换 object 中的数组?

[英]How can i replace an Array inside of an object with new array with multiple objects inside?

我在 react redux state 中有初始值,它基本上是从数据中的 API 中获取的。

let data = {    
    item: [],
    name: "random",
    description: "hello",
    status: "true",
    asset: [],
    version: "0.043",
    }

现在我想用下面的项目数组替换 dat 中的项目数组 []

let item = [
{
    item: 
{
    name: "random",
    description: "General Description",
    status: "true",
    asset: [],
    version: "0.043",
   },
   quantity: "344"
   },
   {
   item: {
   name: "random",
   description: "General Description",
   status: "true",
   asset: [],
   version: "0.043"
   },
   quantity: "434"
   }
   ]

我想要实现的是更新反应 redux state 然后将其发布到 Api

data = {
    item: [
    {
     item: {
        name: "random",
        descr: "General Descr",
        status: "true",
        asset: [],
        version: "0.043",
     },
      quantity: "344"
      },
      {
     item: {
        name: "random",
        descr: "General Descr",
        status: "true",
        asset: [],
        version: "0.043",
     },
      quantity: "344"
      }                     
     ],
    name: "random",
    description: "hello",
    status: "true",
    asset: [],
    version: "0.043",
}

您可以为此使用传播运算符来制作dataitem的副本。 然后您可以在代码的redux部分中使用它

data = { ...data, item: [...data.item, ...item] };

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM