I am trying to group objects from one array based on a value from another array, however I can't seem to find the right way to do it.
I have an array with orders which has a key-value pair like id: 999324 Now I have another array for products that has many objects which has different id's or matching id's like orderlineId: 999324. Now those objects with a matching ID should be grouped together.
Order array
[{
"agreement": null,
"channel": null,
"collection": "Colect 2019",
"comment": "Comment here",
"customerNo": "140159",
"customerOrderReference": "CustomerOReference",
"customerPriceGroup": "1,7",
"erpOrderReference": "1337ORDERREFERENCE",
"externalUrl": null,
"id": 99945333,
},
{
"agreement": null,
"channel": null,
"collection": "Colect 2019",
"comment": "Comment here",
"customerNo": "140159",
"customerOrderReference": "CustomerOReference",
"customerPriceGroup": "1,7",
"erpOrderReference": "1337ORDERREFERENCE",
"externalUrl": null,
"id": 99945334,
]
Products array
[{
"crossReference": null,
"currency": "EU",
"deliverySubBlock": null,
"eanCode": "8717945155406",
"grossLineAmount": 99.99,
"grossWholesalePrice": 99.99,
"id": 1156718740,
"orderlineId": 99945334,
},
{
"crossReference": null,
"currency": "EU",
"deliverySubBlock": null,
"eanCode": "8717945155406",
"grossLineAmount": 99.99,
"grossWholesalePrice": 99.99,
"id": 1156718740,
"orderlineId": 99945334,
},
{
"crossReference": null,
"currency": "EU",
"deliverySubBlock": null,
"eanCode": "8717945155406",
"grossLineAmount": 99.99,
"grossWholesalePrice": 99.99,
"id": 1156718740,
"orderlineId": 99945334
}]
Preferred orders array would be an array with a nested array with all the objects from the products array that matches the orders.id id with products.orderlineId
[{
{
"agreement": null,
"channel": null,
"collection": "Colect 2019",
"comment": "Comment here",
"customerNo": "140159",
"customerOrderReference": "CustomerOReference",
"customerPriceGroup": "1,7",
"erpOrderReference": "1337ORDERREFERENCE",
"externalUrl": null,
"id": 99945334,
"orderLines" [
{
"crossReference": null,
"currency": "EU",
"deliverySubBlock": null,
"eanCode": "8717945155406",
"grossLineAmount": 99.99,
"grossWholesalePrice": 99.99,
"id": 1156748740,
"orderlineId": 99945334
},
{
"crossReference": null,
"currency": "EU",
"deliverySubBlock": null,
"eanCode": "8717945155407",
"grossLineAmount": 9.99,
"grossWholesalePrice": 199.99,
"id": 1156718720,
"orderlineId": 99945334
}
},
{
"agreement": null,
"channel": null,
"collection": "Colect 2019",
"comment": "Comment here",
"customerNo": "140159",
"customerOrderReference": "CustomerOReference",
"customerPriceGroup": "1,7",
"erpOrderReference": "1337ORDERREFERENCE",
"externalUrl": null,
"id": 99945350,
"orderLines" [
{
"crossReference": null,
"currency": "EU",
"deliverySubBlock": null,
"eanCode": "8717945155406",
"grossLineAmount": 99.99,
"grossWholesalePrice": 99.99,
"id": 1156748740,
"orderlineId": 99945350
},
{
"crossReference": null,
"currency": "EU",
"deliverySubBlock": null,
"eanCode": "8717945155407",
"grossLineAmount": 3.99,
"grossWholesalePrice": 299.99,
"id": 1156718720,
"orderlineId": 99945350
}
}
I tried many ways like this
for(let i=0; i<orders.length; i++) {
merged.push({
...products[i],
...(orders.find((itmInner) => itmInner.id === products[i].orderlineId))}
);
}
But that doesn't seem to work correctly.
I hope I made it clear what I try to achieve as it's my first question on Stackoverflow.