按嵌套对象属性分组数组并合并

huangapple go评论71阅读模式
英文:

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}`)
);

huangapple
  • 本文由 发表于 2023年2月6日 18:55:14
  • 转载请务必保留本文链接:https://go.coder-hub.com/75360384.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定