简体   繁体   English

如何使用扩展运算符完全合并两个树对象?

[英]How can I fully merge two tree objects with spread operator?

I am trying to merge two tree objects into one with the spread operator, but I am not getting the correct merged result.我正在尝试使用扩展运算符将两个树对象合并为一个,但我没有得到正确的合并结果。 How can I fully merge two tree objects with spread operator?如何使用扩展运算符完全合并两个树对象?

 const tree1 = [{ comments: [{ text: "This a comment for case law 84", id: "84" }, { text: "This a comment for case law 89", id: "89" }], children: [{ comments: [{ text: "This a comment for case law 70", id: "70" }], children: [{ comments: [{ text: "This a comment for case law 83", id: "83" }] }] }] }]; const tree2 = [{ comments: [{ text: "This a comment for case law 184", id: "184" }], children: [{ comments: [{ text: "This a comment for case law 170", id: "170" }], children: [{ comments: [{ text: "This a comment for case law 183", id: "183" }] }] }] }]; const mergedTrees = [{...tree2, ...tree1 }]; console.log("mergedTrees", mergedTrees);

The problem is while merging the properties with the same key get overwritten.问题是在合并具有相同键的属性时会被覆盖。 The rightmost property has the highest precedence.最右边的属性具有最高的优先级。 What I need to get this kind of merge?我需要什么来获得这种合并? : :

{
  "0": {
    "comments": [
      {
        "text": "This a comment for case law 84",
        "id": "84"
      },
      {
        "text": "This a comment for case law 89",
        "id": "89"
      },
      {
        "text": "This a comment for case law 184",
        "id": "184"
      }
    ],
    "children": [
      {
        "comments": [
          {
            "text": "This a comment for case law 70",
            "id": "70"
          },
          {
            "text": "This a comment for case law 170",
            "id": "170"
          }
        ],
        "children": [
          {
            "comments": [
              {
                "text": "This a comment for case law 83",
                "id": "83"
              },
              {
                "text": "This a comment for case law 183",
                "id": "183"
              }
            ]
          }
        ]
      }
    ]
  }
}

if it's not possible with the spread operator and there is another way to make it, please let me know.如果传播运算符无法做到这一点并且有另一种方法可以做到这一点,请告诉我。

adding a link for tries: https://stackblitz.com/edit/fffika?file=index.ts添加尝试链接: https://stackblitz.com/edit/fffika?file=index.ts

You could merge the arrays index-wise and take the same approach for nested children.您可以按索引合并 arrays 并对嵌套子项采用相同的方法。

 const merge = (a, b) => [a, b].reduce((r, array) => { array.forEach(({ children, ...o }, i) => { r[i]??= { }; Object.entries(o).forEach(([k, v]) => (r[i][k]??= []).push(...v)); if (children) r[i].children = merge(r[i].children || [], children); }); return r; }, []), tree1 = [{ comments: [{ text: "This a comment for case law 84", id: "84" }], news: [{ text: "This news 1 ", id: "1" }], children: [{ comments: [{ text: "This a comment for case law 70", id: "70" }], news: [{ text: "This news 2 ", id: "2" }, { text: "This news 3 ", id: "3" }], children: [{ comments: [{ text: "This a comment for case law 83", id: "83" }], news: [{ text: "This news 4 ", id: "4" }] }] }] }], tree2 = [{ comments: [{ text: "This a comment for case law 184", id: "184" }], news: [{ text: "This news 12 ", id: "12" }, { text: "This news 13 ", id: "13" }], children: [{ comments: [{ text: "This a comment for case law 170", id: "170" }], news: [{ text: "This news 22 ", id: "22" }, { text: "This news 33", id: "33" }], children: [{ comments: [{ text: "This a comment for case law 183", id: "183" }], news: [{ text: "This news 122 ", id: "122" }, { text: "This news 133 ", id: "133" }] }] }] }] result = merge(tree1, tree2); console.log(result);
 .as-console-wrapper { max-height: 100%;important: top; 0; }

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

相关问题 Javascript:使用spread运算符合并两个对象 - Javascript: Merge two objects with spread operator 具有一个排除的扩展运算符以合并对象 - Spread operator with one exclusion to merge objects 有没有办法使用扩展运算符合并两个对象,使得结果具有一个 object 的键,但第二个的值? - Is there way to use the spread operator to merge two objects such that the result has the keys of one object, but the values of the second? 如何在 ES6 中的两个扩展运算符之间设置逻辑运算符 - How can I set a logical operator between two spread operators in ES6 如何在javascript扩展运算符中与嵌套键合并? - How to merge with nested key in javascript spread operator? 如何使用扩展运算符 (ES6) 根据对象的 id 合并对象列表 - How to merge lists of objects based on their id's using the spread operator (ES6) 如何在嵌套的 javascript 对象上使用扩展运算符? - How to use spread operator on nested javascript objects? 如何使用扩展运算符将属性添加到 Typescript 中的内部 Object ? - How can i add a property to an inner Object in Typescript with spread operator? 我怎样才能在 C# 中模仿 Javascript “扩展运算符”? - How can I mimic a Javascript "spread operator" in C#? 如何使用扩展运算符将多个数据存储在状态中 - How can I store multiple data in state using spread operator
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM