为了账号安全,请及时绑定邮箱和手机立即绑定

如何通过合并的方式合并两个对象?

如何通过合并的方式合并两个对象?

皈依舞 2021-11-25 19:20:51
我遇到了这样的问题,很长时间都无法解决。所以,有两个多维对象。我必须通过将这两个对象合并为一个对象来执行将这两个对象合并为一个最终对象的操作。问题是对象是多维的,我无法通过合并正确合并它们。通过这个链接你可以看到对象的数据,下面我将给出最终结果的视图。[  {    text: 'A', children: [{        text: 'B', children: [{            text: 'C',            children: [              {text: 'B [43]', id: '43'},               {text: 'B [93]', id: '93'},              {text: 'B [11]', id: '11'},            ]}        ]}    ]  },  {    text: 'D', children: [{        text: 'M', children: [{            text: 'N', children: [              {text: 'M [66]', id: '66'}            ]}        ]    }]  },  {    text: 'W', children: [      {        text: 'M', children: [{            text: 'K', children: [              {text: 'M [48]', id: '48'},              {text: 'M [58]', id: '58'}            ]        }]      }, {        text: 'T', children: [{            text: 'K', children: [{text: 'S [78]', id: '78'}]        }]      }    ]  }];无论如何,我添加源对象:const data_1 = [{    text: 'A', children: [{        text: 'B', children: [{            text: 'C',            children: [              {text: 'B [43]', id: '43'},               {text: 'B [11]', id: '11'},            ]}        ]}    ]  },  {    text: 'W', children: [      {        text: 'M', children: [{            text: 'K', children: [{text: 'M [48]', id: '48'}]        }]      }, {        text: 'T', children: [{            text: 'K', children: [{text: 'S [78]', id: '78'}]        }]      }    ]  }];const data_2 = [{    text: 'A', children: [{        text: 'B', children: [{            text: 'C',            children: [              {text: 'B [93]', id: '93'},              {text: 'B [11]', id: '11'},            ]}        ]}    ]  },  {    text: 'D', children: [{        text: 'M', children: [{            text: 'N', children: [              {text: 'M [66]', id: '66'}            ]}        ]    }]  },  在这里,所有元素的联合发生在它们出现在一个对象中而在另一个对象中不存在的地方。我知道这项任务并不容易,它可能无法以这种格式获得解决方案,但我恳求您不要路过,至少提供建议、分享经验、提供替代方案、提供类似信息等。解决这个问题对我来说非常重要,我将非常感谢任何帮助,意思是建议,有用的信息,当然还有解决方案。
查看完整描述

2 回答

?
慕码人8056858

TA贡献1803条经验 获得超6个赞

您可以减少数组并检查是否text存在相同的属性,然后检查 children 属性,否则将对象推送到实际结果集。


这种方法会改变左数组a。


const

    merge = (a, b) => {

        b.forEach(o => {

            var item = a.find(q => o.text === q.text);

            if (item) {

                if (o.children) [item.children = item.children || [], o.children].reduce(merge);

            } else {

                a.push(o);

            }

        });

        return a;

    };


var data1 = [{ text: 'A', children: [{ text: 'B', children: [{ text: 'C', children: [{ text: 'B [43]', id: '43' }, { text: 'B [11]', id: '11' }] }] }] }, { text: 'W', children: [{ text: 'M', children: [{ text: 'K', children: [{ text: 'M [48]', id: '48' }] }] }, { text: 'T', children: [{ text: 'K', children: [{ text: 'S [78]', id: '78' }] }] }] }],

    data2 = [{ text: 'A', children: [{ text: 'B', children: [{ text: 'C', children: [{ text: 'B [93]', id: '93' }, { text: 'B [11]', id: '11' }] }] }] }, { text: 'D', children: [{ text: 'M', children: [{ text: 'N', children: [{ text: 'M [66]', id: '66' }] }] }] }, { text: 'W', children: [{ text: 'M', children: [{ text: 'K', children: [{ text: 'M [58]', id: '58' }] }] }] }];



[data1, data2].reduce(merge);

console.log(data1);

.as-console-wrapper { max-height: 100% !important; top: 0; }


查看完整回答
反对 回复 2021-11-25
?
摇曳的蔷薇

TA贡献1793条经验 获得超6个赞

对于每个级别,您可以对公共对象进行分组(基于它们的text值),如果其中任何一个具有children属性,则递归合并它们:


const data_1 = [{ "text": "A", "children": [{ "text": "B", "children": [{ "text": "C", "children": [{ "text": "B [43]", "id": "43" }, { "text": "B [11]", "id": "11" }] }] }] }, { "text": "W", "children": [{ "text": "M", "children": [{ "text": "K", "children": [{ "text": "M [48]", "id": "48" }] }] }, { "text": "T", "children": [{ "text": "K", "children": [{ "text": "S [78]", "id": "78" }] }] }] }];

const data_2 = [{ "text": "A", "children": [{ "text": "B", "children": [{ "text": "C", "children": [{ "text": "B [93]", "id": "93" }, { "text": "B [11]", "id": "11" }] }] }] }, { "text": "D", "children": [{ "text": "M", "children": [{ "text": "N", "children": [{ "text": "M [66]", "id": "66" }] }] }] }, { "text": "W", "children": [{ "text": "M", "children": [{ "text": "K", "children": [{ "text": "M [58]", "id": "58" }] }] }] }];


function mergeArrays(arr1 = [], arr2 = []) {

  const pairs = [...arr1, ...arr2].reduce((acc, curr) => {

    acc[curr.text] = acc[curr.text] || [];

    acc[curr.text].push(curr);

    return acc;

  }, {});


  return Object.values(pairs).map(([p1, p2 = {}]) => {

    const res = { ...p1, ...p2 };

    if (p1.children || p2.children) res.children = mergeArrays(p1.children, p2.children);

    return res;

  });

}


console.log(mergeArrays(data_1, data_2));

请注意,这种方法不会改变原始数组。


查看完整回答
反对 回复 2021-11-25
  • 2 回答
  • 0 关注
  • 248 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信