I am trying to merge an array of products
with the same order_id
but also adding all the object of the second products
array. Here is an example of my orders.
const orders = [
{
"order_details": {
},
"order_id": "1",
"order_status": "Pending",
"order_type": "drugs",
"products": [
{
"name": "product xyz"
}
],
},
{
"order_details": {
},
"order_id": "2",
"order_status": "Pending",
"order_type": "Prescriptions",
"products": [
{
"name": "product abc"
}
],
},
{
"order_details": {
},
"order_id": "1",
"order_status": "Pending",
"order_type": "goods",
"products": [
{
"name": "product ghj"
}
],
},
]
and this is what I want to transform it into
const orders = [
{
"order_details": {
},
"order_id": "1",
"order_status": "Pending",
"order_type": "drugs & goods",
"products": [
{
"name": "product xyz"
},
{
"name": "product ghj"
}
],
},
{
"order_details": {
},
"order_id": "2",
"order_status": "Pending",
"order_type": "Prescriptions",
"products": [
{
"name": "product abc"
}
],
}
]
CodePudding user response:
Convert your array to an object where the keys are equal to the order_id
property of each order. That way you can easily check if there any duplicates just by looking up the order_id
key.
For the order_type
I would recommend that you use an array of strings. Let's say you have 5 duplicates all with different types, then things will look like "drugs & prescriptions & goods & clothing & footwear"
.
const orders = [{
"order_details": {},
"order_id": "1",
"order_status": "Pending",
"order_type": "drugs",
"products": [{
"name": "product xyz"
}],
},
{
"order_details": {},
"order_id": "2",
"order_status": "Pending",
"order_type": "Prescriptions",
"products": [{
"name": "product abc"
}],
},
{
"order_details": {},
"order_id": "1",
"order_status": "Pending",
"order_type": "goods",
"products": [{
"name": "product ghj"
}],
},
];
function combineOrders(orders) {
const orderIndex = {};
for (const { order_id, order_type, products, ...rest } of orders) {
if (typeof orderIndex[order_id] !== 'undefined') {
const order = orderIndex[order_id];
const orderType = Array.isArray(order.order_type)
? [...order.order_type, order_type]
: [order.order_type, order_type];
const orderProducts = [...order.products, ...products];
orderIndex[order_id] = {
...rest,
order_id,
order_type: orderType,
products: orderProducts
}
} else {
orderIndex[order_id] = {
order_id,
order_type,
products,
...rest
}
}
}
return Object.values(orderIndex);
}
const newOrders = combineOrders(orders);
console.log(newOrders);
CodePudding user response:
Try this:
const orders = [
{
"order_details": {
},
"order_id": "1",
"order_status": "Pending",
"order_type": "drugs",
"products": [
{
"name": "product xyz"
}
],
},
{
"order_details": {
},
"order_id": "2",
"order_status": "Pending",
"order_type": "Prescriptions",
"products": [
{
"name": "product abc"
}
],
},
{
"order_details": {
},
"order_id": "1",
"order_status": "Pending",
"order_type": "goods",
"products": [
{
"name": "product ghj"
}
],
},
];
const result = [];
for (let order of orders){
const exist = result.find(e => e.order_id === order.order_id);
if(exist){
exist.products.push(...order.products);
if(!exist.order_type.split(' ').some(e => e === order.order_type))
exist.order_type = ' & ' order.order_type;
}
else result.push(order);
}
console.log(result);