如何使用 lodash javascript 对对象中的数组数据进行分组

我想使用 lodash javscript 对数据进行分组。首先我尝试使用 groupBy machine_id 然后我总结总价值

这是我的数据

const data = [
  {
    id: 1,
    machine_id: 1,
    work_id: 1,
    total: 10
  },
  {
    id: 2,
    machine_id: 1,
    work_id: 2,
    total: 15
  },
  {
    id: 3,
    machine_id: 2,
    work_id: 3,
    total: 10
  },
  {
    id: 4,
    machine_id: 1,
    work_id: 1,
    total: 15
  },
]

我想对 machine_id 进行分组,然后我想通过 machine_id 求和,然后我想对每个 work_id 求和

我想这样输出

[
  {
      "machine_id": 1,
      "sum_total": 25
      "work_group": [
        // In this work group I want to sum total each work_id 
        {
          work_id: 1
          sum_total: 25
        },
          {
          work_id: 2
          sum_total: 15
        }
      ]
    },
    {
      "machine_id": 2,
      "sum_total": 10
      "work_group": [
        {
          work_id: 3
          sum_total: 10
        }
      ]
    },
]

这就是我尝试的

let groupData = _(data)
.groupBy("machine_id")
.map((data, machine_id) => ({
    machine_id,
    sum_total: _.sumBy(data, item => Number(item.total_time))
}))
.value();

我的输出是这样的:

[
  {
      "machine_id": 1,
      "sum_total": 25

    },
    {
      "machine_id": 2,
      "sum_total": 10

    },
]

如何通过 work_id 向下钻取总和

湿婆KV

用户reduceObject.values将简化

const data = [
  {
    id: 1,
    machine_id: 1,
    work_id: 1,
    total: 10
  },
  {
    id: 2,
    machine_id: 1,
    work_id: 2,
    total: 15
  },
  {
    id: 3,
    machine_id: 2,
    work_id: 3,
    total: 10
  },
  {
    id: 4,
    machine_id: 1,
    work_id: 1,
    total: 15
  }
];

const updated = Object.values(
  data.reduce((acc, curr) => {
    if (curr.machine_id in acc) {
      const work_group = [
        ...acc[curr.machine_id].work_group,
        { work_id: curr.work_id, sum_total: curr.total }
      ].reduce((wg_acc, wg_curr) => {
        wg_acc[wg_curr.work_id] =
          wg_curr.work_id in wg_acc
            ? {
                ...wg_acc[wg_curr.work_id],
                sum_total: wg_acc[wg_curr.work_id].sum_total + wg_curr.sum_total
              }
            : { ...wg_curr };
        return wg_acc;
      }, {});
      acc[curr.machine_id] = {
        ...acc[curr.machine_id],
        sum_total: acc[curr.machine_id].sum_total + curr.total,
        work_group: Object.values(work_group)
      };
    } else {
      acc[curr.machine_id] = {
        machine_id: curr.machine_id,
        sum_total: curr.total,
        work_group: [{ work_id: curr.work_id, sum_total: curr.total }]
      };
    }

    return acc;
  }, {})
);

console.log(updated);

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何使用lodash对对象数组中的属性进行分组?

使用 Lodash 对对象进行分组和求和数组

如何基于Javascript中的多个数组属性对对象数组进行分组

如何基于键对对象的JavaScript数组进行分组

如何使用Vanilla或lodash对对象数组进行排序

如何基于Javascript中的多个键对对象数组进行分组?

如何基于香草javascript中的属性对对象数组进行分组

如何使用es6或lodash在javascript中按索引对两个数组进行分组?

使用Lodash通过多个属性对对象数组进行分组

在javascript中对对象数组进行分组

使用lodash通过多个键对对象进行分组

如何在javascript中对对象元素进行分组

如何使用 lodash 或 javascript 按数组分组

使用 lodash 按嵌套属性对对象数组进行排序

使用Lodash按值对对象数组进行排序

javascript / lodash:由于子树对象,如何对对象进行递归深度获取

如何在javascript中对对象数组进行排序?

在 JavaScript 中同时分组和转换数据(使用 Lodash)

使用javascript / lodash遍历嵌套对象的数组

如何通过基于特定字段值对对象进行分组来过滤 javascript 对象数组?

使用Lodash遍历JavaScript对象中的属性

使用 JavaScript 对对象数组进行排序

在javascript中按id对对象数组进行分组

Javascript-使用Lambda按日期对对象数组进行分组

如何根据对象中的属性对对象数组进行分组

使用lodash对JavaScript中的多行数据进行排序

如何使用lodash按日期字段对对象进行排序?

如何使用javascript或lodash从对象数组中删除不匹配的对象

如何使用jQuery或JavaScript对对象数组进行排序