亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何通過合并的方式合并兩個對象?

如何通過合并的方式合并兩個對象?

皈依舞 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 關注
  • 278 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號