简体   繁体   English

如何在对象的嵌套数组中添加对象而不改变原始源

[英]how to add object in nested array of objects without mutating original source

I have multi nested array of objects, in which I need to merge objects in the grandChildren based on the id without mutation我有多个嵌套的对象数组,其中我需要根据没有突变的 id 合并 grandChildren 中的对象

Details ......细节 ......

###Example ###例子

  let arr1 = {
  "initiateLevel": true,
  "parent": [
    {
      "name": "level1",
      "childrens": [
        {
          "group": "Level-group",
          "grandChildrens": [
            {
              "id": 21,
              "technology": "sp1",
              "path": "l2"
            },
            {
              "id": 22,
              "technology": "sp2",
              "path": "l2"
            }
          ]
        }
      ]
    },
    {
      "name": "level2",
      "childrens": [
        {
          "group": "Level-group-2",
          "grandChildrens": [
            {
              "id": 121,
              "technology": "sp12",
              "path": "l4"
            },
            {
              "id": 122,
              "technology": "sp22",
              "path": "l4"
            }
          ]
        }
      ]
    }
  ]
}

ex: Below object needs to merged with the array based on the id例如:下面的对象需要与基于 id 的数组合并

 let newobj=  
 [
      {
        "id": 22,
        "reason": "reason 2",
        "phase": "phase 2",
        "reviewer": "by user 2",
        "date": "date 2"
      },
      {
        "id": 21,
        "reason": "reason 1",
        "phase": "phase 1",
        "reviewer": "by user 1",
        "date": "date 1"
      }
    ]

expected output:预期输出:

{
  "initiateLevel": true,
  "parent": [
    {
      "name": "level1",
      "childrens": [
        {
          "group": "Level-group",
          "grandChildrens": [
            {
              "id": 21,
              "technology": "sp1",
              "path": "l2",
              "reason": "reason 1",
              "phase": "phase 1",
              "reviewer": "by user 1",
              "date": "date 1"
            },
            {
              "id": 22,
              "technology": "sp2",
              "path": "l2",
              "reason": "reason 2",
              "phase": "phase 2",
              "reviewer": "by user 2",
              "date": "date 2"
            }
          ]
        }
      ]
    },
    {
      "name": "level2",
      "childrens": [
        {
          "group": "Level-group-2",
          "grandChildrens": [
            {
              "id": 121,
              "technology": "sp12",
              "path": "l4"
            },
            {
              "id": 122,
              "technology": "sp22",
              "path": "l4"
            }
          ]
        }
      ]
    }
  ]
}

I tried to like this.我试着喜欢这个。 but it's not working但它不工作

const merge = (y, z) => {
  y.parent.forEach((element) => {
    element.childrens.forEach((x) => {
      x.grandChildrens.forEach((test) => {
        const reviewIndex = z.findIndex(
          (reviewItem) => reviewItem.id === test.id
        );
        if(reviewIndex>=0)
        {
         return  {...test, ...z[reviewIndex]}  
        }
        
      });
    });
  });
};

merge(arr1,newobj)

How to merge the object based on the id without mutation.如何根据 id 合并对象而不进行变异。

Use Array.prototype.map instead, forEach gets no returns改用Array.prototype.mapforEach没有回报

 let newobj = [ { id: 22, reason: 'reason 2', phase: 'phase 2', reviewer: 'by user 2', date: 'date 2' }, { id: 21, reason: 'reason 1', phase: 'phase 1', reviewer: 'by user 1', date: 'date 1' } ]; let arr1 = { initiateLevel: true, parent: [ { name: 'level1', childrens: [ { group: 'Level-group', grandChildrens: [ { id: 21, technology: 'sp1', path: 'l2', reason: 'reason 1', phase: 'phase 1', reviewer: 'by user 1', date: 'date 1' }, { id: 22, technology: 'sp2', path: 'l2', reason: 'reason 2', phase: 'phase 2', reviewer: 'by user 2', date: 'date 2' } ] } ] }, { name: 'level2', childrens: [ { group: 'Level-group-2', grandChildrens: [ { id: 121, technology: 'sp12', path: 'l4' }, { id: 122, technology: 'sp22', path: 'l4' } ] } ] } ] }; const merge = (y, z) => { const parent = y.parent.map((element) => { return { ...element, childrens: element.childrens.map((x) => { return { ...x, grandChildrens: x.grandChildrens.map((test) => { return { ...test, ...z.find((reviewItem) => reviewItem.id === test.id) }; }) }; }) } }); return { ...y, parent }; }; const arr2 = merge(arr1, newobj); console.log(arr2);

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

相关问题 在不改变原始数组的情况下在数组中添加对象的算法 - algorithm to add up objects in an array without mutating the original array 如何在不更改原始数组的情况下过滤嵌套数组? - How can I filter a nested array without mutating the original array? 为什么map()在嵌套数组中改变原始对象? - why map() is mutating original objects in a nested array? 如何在不更改原始数组的情况下破坏对象属性? - How do i destruct object properties without mutating original array? 如何在不更改原始数组的情况下更改对象Key - How to change an object Key without mutating original array 如何在不变异的情况下更新对象数组的嵌套数组的状态 - How to update state of nested array of array of objects without mutating ReactJs 如何在不发生变异的情况下将对象添加到数组? - ReactJs how can i add object to array without mutating? 如何在Redux中更改嵌套对象而不对其进行更改 - How to change nested Object without mutating it in Redux 如何在不改变原始数组的情况下对数组进行排序? - How can you sort an array without mutating the original array? 如何将数组传递给函数而不改变原始数组? - How to pass an array to a function without mutating the original array?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM