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; }

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; }

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 個),這將在性能級別上崩潰。在這種情況下,哈希表答案可能是更好的方法。
添加回答
舉報