我有 2 个具有父数组和子数组的分组数组,我想合并这两个具有相同父数组的数组,但我仅复制具有唯一顺序的子数组,我在互联网上搜索了许多文章,但尚未找到解决方案。请帮我。这是我的代码
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>
您会看到2019 年 4 月 26 日组中有重复的
{
vertical_id: "3",
vertical_category: "C"
},
我怎样才能删除重复的?请帮助我,谢谢
尚方宝剑之说
烙印99
相关分类