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

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

如何使兩個對象類型數組相交,包括合并它們的項目?

如何使兩個對象類型數組相交,包括合并它們的項目?

素胚勾勒不出你 2024-01-18 16:21:41
我有兩個長度相等的數組,每個數組都包含對象數據。這是第一個數組的示例代碼......const array1 = [{  key: '5',  value: '550',}, {  key: '6',  value: '750',}];這是第二個的代碼......const array2 = [{  type: 'Job',  status: 'Finished',  key : '5',}, {  type: 'Ticket',  status: 'Processing',  key : '6',}];為了進一步處理我的數據,我需要兩個數組的交集及其相應的對象項被合并。結果應該是這樣的......[{  key: '5',  value: '550',  type: 'Job',  status: 'Finished',}, {  key: '6',  value: '750',  type: 'Ticket',  status: 'Processing',}]到目前為止我想出的是..array1.forEach(function (element) {  array2.forEach(function (element) {    return {      type: 'Ticket',      status: 'Processing'    };  });});我不知道如何創建預期的結果。我的問題的解決方案是什么樣的?
查看完整描述

3 回答

?
一只甜甜圈

TA貢獻1836條經驗 獲得超5個贊

您可以將對象存儲在哈希表中,key并將另一個數組與哈希表中的數據合并。


const

    array1 = [{ key: '5', value: '550' }, { key: '6', value: '750' }],

    array2 = [{ type: 'Job', status: 'Finished', key: '5' }, { type: 'Ticket', status: 'Processing', key: '6' }],

    temp = Object.fromEntries(array2.map(o => [o.key, o])),

    result = array1.map(o => ({ ...o, ...temp[o.key] }));


console.log(result);

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


查看完整回答
反對 回復 2024-01-18
?
紅糖糍粑

TA貢獻1815條經驗 獲得超6個贊

利用Array.prototype.map它可以將附加target對象傳遞給...,這里可以使用第二個數組,從中檢索相應的合并項。

合并是通過Object.assign將兩個數組項合并到一個新創建的對象中來完成的,以便不改變任一涉及數組的任何合并項......

const array1 = [{

? key: '5',

? value: '550',

}, {

? key: '6',

? value: '750',

}];


const array2 = [{

? type: 'Job',

? status: 'Finished',

? key : '5',

}, {

? type: 'Ticket',

? status: 'Processing',

? key : '6',

}];


function mergeWithSameIndexItemFromBoundArray(item, idx) {

? const boundArray = this;

? return Object.assign({}, item, boundArray[idx]);

}


console.log(

? 'merge result of array2 and array1 ...',

? array2.map(mergeWithSameIndexItemFromBoundArray, array1)

);

console.log(

? 'merge result of array1 and array2 ...',

? array1.map(mergeWithSameIndexItemFromBoundArray, array2)

);

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

如果存在不匹配的商品訂單數組,可以基于上述方法向 提供額外的目標對象map

這次不是第二個數組,而是它的一個變換;一個對象,它使用標識所有項目的屬性名稱將第二個數組的所有項目映射到它。對于OP的示例,該屬性名稱是key

因此,需要編寫一個附加函數來完成此映射任務。下面的下一個示例選擇一種基于 的方法Array.prototype.reduce。

此外,必須重寫映射器函數,以便通過簡單地使用數組項的key屬性而不是之前的數組索引來利用之前創建的綁定映射...

const array1 = [{

? key: '5',

? value: '550',

}, {

? key: '7',

? value: '320',

}, {

? key: '6',

? value: '750',

}];


const array2 = [{

? type: 'Ticket',

? status: 'Processing',

? key : '6',

}, {

? type: 'Job',

? status: 'Finished',

? key : '5',

}, {

? type: 'Task',

? status: 'Pending',

? key : '7',

}];


function createKeyBasedItemMap(map, item) {

? map[item.key] = item;

? return map;

}

function mergeWithKeyBasedItemFromBoundMap(item) {

? return Object.assign({}, item, this[item.key]);

}


console.log(

? 'map-based merge of unordered array2 and array1 items ...',

? array2.map(

? ? mergeWithKeyBasedItemFromBoundMap,

? ? array1.reduce(createKeyBasedItemMap, {})

? )

);

console.log(

? 'map-based merge of unordered array1 and array2 items ...',

? array1.map(

? ? mergeWithKeyBasedItemFromBoundMap,

? ? array2.reduce(createKeyBasedItemMap, {})

? )

);

console.log('\n');


console.log(

? 'proof of concept ... the item-map based on array1 ...',

? array1.reduce(createKeyBasedItemMap, {})

);

console.log(

? 'proof of concept ... the item-map based on array2 ...',

? array2.reduce(createKeyBasedItemMap, {})

);

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


查看完整回答
反對 回復 2024-01-18
?
心有法竹

TA貢獻1866條經驗 獲得超5個贊

聽起來您需要通過鍵將兩個數組中的對象組合起來。數組方法是一個不錯的選擇,但您還應該研究其他方法。

const?combinedItemsArray?=?array1.map(item1?=>?{
???????const?matchingItem?=?array2.find(item2?=>?item2.key?===?item1.key)?
???????return?{...item1,?...matchingItem?}
})

這使用擴展運算符將項目的值與匹配的鍵組合起來。

需要考慮的一些事項:

  • Array.find 僅匹配數組 2 中滿足結果的第一項。因此,如果數組中的項具有重復的鍵,則需要對其進行修改。

  • 如果數組中有很多項目(10 個或 1000 個),這將在性能級別上崩潰。在這種情況下,哈希表答案可能是更好的方法。

查看完整回答
反對 回復 2024-01-18
  • 3 回答
  • 0 關注
  • 156 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號

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