英文:
Group array by nested object property and merge
问题
以下是您提供的代码片段的中文翻译:
我有一个具有以下结构的数组:
BasketDish
[
{
id: string;
quantity: number;
Dish: AsyncItem<Dish | undefined>;
basketID: string;
createdAt?: string | null;
updatedAt?: string | null;
basketDishDishId?: string | null;
}
]
Dish
[
{
id: string;
name: string;
price: number;
},
{
id: string;
name: string;
price: number;
}
]
我需要按Dish.id对数组进行分组,然后创建一个数组,累积数量和总价格
从:
[
{
id: 1,
name: BBQ Burger,
price: 17
},
{
id: 2,
name: CheeseBurger,
price: 15
},
{
id: 2,
name: CheeseBurger,
price: 15
},
]
到期望的结果:
[
{
id: 1,
name: BBQ Burger,
price: 17,
total: 17,
quantity: 1
},
{
id: 2,
name: CheeseBurger,
price: 15,
total: 30,
quantity: 2
},
]
我尝试了很多次使用groupBy和merge,但无法弄清楚
### 更新
感谢 @BioStunt
只需更新您的解决方案,而不是按id分组,我必须按*Dish.id*分组
/**
* 合并具有相同id的Dishes
*/
const groupedItems = chain(basketDishes)
/** 按“id”键分组项目 */
.groupBy(a => a.Dish?.id)
/** 转换分组的项目 */
.map((items, id) => ({
id: id,
dishId: items[0]?.Dish?.id,
name: items[0].Dish?.name,
quantity: items.length,
total: items.reduce((acc, item) => acc + item.Dish?.price!, 0),
}))
/** 获取链的结果 */
.value();
希望这对您有所帮助!
英文:
I have an array with following structure:
BasketDish
[
{
id: string;
quantity: number;
Dish: AsyncItem<Dish | undefined>;
basketID: string;
createdAt?: string | null;
updatedAt?: string | null;
basketDishDishId?: string | null;
}
]
Dish
[
{
id: string;
name: string;
price: number;
},
{
id: string;
name: string;
price: number;
}
]
I need to group the array by Dish.id and then create an array which accumulates the quantity and total price
From:
[
{
id: 1,
name: BBQ Burger,
price: 17
},
{
id: 2,
name: CheeseBurger,
price: 15
},
{
id: 2,
name: CheeseBurger,
price: 15
},
]
To expected result:
[
{
id: 1,
name: BBQ Burger,
price: 17,
total: 17,
quantity: 1
},
{
id: 2,
name: CheeseBurger,
price: 15,
total: 30,
quantity: 2
},
]
I tried a lot with groupBy and merge, but couldn't figure it out
UPDATE
Thanks @BioStunt
Just needed to update your solution instead of group by id i had to group by Dish.id
/**
* Merge Dishes with same id
*/
const groupedItems = chain(basketDishes)
/** group items by key "id" */
.groupBy(a => a.Dish?.id)
/** convert grouped items */
.map((items, id) => ({
id: id,
dishId: items[0]?.Dish?.id,
name: items[0].Dish?.name,
quantity: items.length,
total: items.reduce((acc, item) => acc + item.Dish?.price!, 0),
}))
/** get result of chain */
.value();
答案1
得分: 1
如果我正确理解了你的问题,你应该使用lodash
中的chain + groupBy
方法。在你的情况下,你有一个基本的项目数组:
const basketDish = [
{ id: 1, name: 'BBQ Burger', price: 17 },
{ id: 2, name: 'CheeseBurger', price: 15 },
{ id: 2, name: 'CheeseBurger', price: 15 },
];
要合并这些数据,使用以下代码:
const groupedItems = chain(basketDish)
/** 按键"id"分组项目 */
.groupBy('id')
/** 转换分组后的项目 */
.map((items, id) => ({
id: Number(id),
name: items[0].name,
quantity: items.length,
total: items.reduce((acc, item) => acc + item.price, 0)
}))
/** 获取链式操作的结果 */
.value();
以下是控制台输出:
groupedItems.forEach((item) =>
console.log(`${item.quantity} * ${item.name} | ${item.total}`)
);
英文:
If I correctly understood your question, you should use chain + groupBy from lodash
.
In your case you have base item array:
const basketDish = [
{ id: 1, name: 'BBQ Burger', price: 17 },
{ id: 2, name: 'CheeseBurger', price: 15 },
{ id: 2, name: 'CheeseBurger', price: 15 },
];
To merge this data use this:
const groupedItems = chain(basketDish)
/** group items by key "id" */
.groupBy('id')
/** convert grouped items */
.map((items, id) => ({
id: Number(id),
name: items[0].name,
quantity: items.length,
total: items.reduce((acc, item) => acc + item.price, 0)
}))
/** get result of chain */
.value();
Below is the console output:
groupedItems.forEach((item) =>
console.log(`${item.quantity} * ${item.name} | ${item.total}`)
);
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论