i have 2 grouped array that have parent and child array, i want to merge these 2 array that have same parent, but the child that i copy only that have unique order, i search for many articles in internet but not yet found the solution. please help me. there is my code
var groupOrder = [
{
group_time_str: "25 Apr 2019",
orders: [
{
vertical_id: "1",
vertical_category: "A"
},
{
vertical_id: "2",
vertical_category: "B"
}
]
},
{
group_time_str: "26 Apr 2019",
orders: [
{
vertical_id: "1",
vertical_category: "A"
},
{
vertical_id: "2",
vertical_category: "B"
},
{
vertical_id: "3",
vertical_category: "C"
}
]
}
];
var groupOrder2 = [
{
group_time_str: "26 Apr 2019",
orders: [
{
vertical_id: "3",
vertical_category: "C"
},
{
vertical_id: "4",
vertical_category: "D"
}
]
},
{
group_time_str: "27 Apr 2019",
orders: [
{
vertical_id: "1",
vertical_category: "A"
},
{
vertical_id: "2",
vertical_category: "B"
}
]
}
];
const combine = [...groupOrder, ...groupOrder2];
const groupBy = combine.reduce((acc, cur) => {
acc[cur.group_time_str]
? (acc[cur.group_time_str].orders = [
...acc[cur.group_time_str].orders,
...cur.orders
])
: (acc[cur.group_time_str] = cur);
return acc;
}, {});
console.log(groupBy);
body {
font-family: sans-serif;
}
<h1>Grouped Order</h1>
you see that on group 26 Apr 2019 there is duplicate
{
vertical_id: "3",
vertical_category: "C"
},
how i can remove the duplicate one? please help me, thank you