简体   繁体   English

JavaScript 使用 Lodash 通过一个或两个键对对象数组进行分组和变换

[英]JavaScript group and transform array of objects by one or two keys using Lodash

How can I group by Date and transform the following data如何按Date分组并转换以下数据

// PROVIDED INPUT
[
  {
    "Date": "2021/Dec/01",
    "Media category": "Canvas",
    "Printed square meters": 244.58
  },
  {
    "Date": "2021/Dec/01",
    "Media category": "Film",
    "Printed square meters": 152.62
  },
  {
    "Date": "2021/Dec/01",
    "Media category": "Heavy paper > 200gsm",
    "Printed square meters": 256.02000000000004
  },
  {
    "Date": "2021/Dec/02",
    "Media category": "Textile",
    "Printed square meters": 144.95999999999998
  },
  {
    "Date": "2021/Dec/02",
    "Media category": "Thick film > 200 um",
    "Printed square meters": 153.37
  },
  {
    "Date": "2021/Dec/02",
    "Media category": "Paper",
    "Printed square meters": 217.2
  }, ...
]

into this with JavaScript.用 JavaScript 进入这个。 I tried to accomplish this with Lodash's groupBy() and mergeWith() , but I am not getting anywhere:我试图用 Lodash 的groupBy()mergeWith()来完成这个,但我没有得到任何地方:

// DESIRED OUTPUT
[
  {
    "Date": "2021/Dec/01",
    "Canvas": 244.58,
    "Film": 152.62
    "Heavy paper > 200gsm": 256.02000000000004
  },
  {
    "Date": "2021/Dec/02",
    "Textile": 144.95999999999998,
    "Thick film > 200 um": 153.37
    "Paper": 217.2
  }, ...
]

Also, is it possible to group by two keys - ie Date and Printer id :此外,是否可以按两个键分组 - 即DatePrinter id

// PROVIDED INPUT
[
  {
    "Date": "2021/Dec/01",
    "Printer id": "700",
    "Media category": "Canvas",
    "Printed square meters": 244.58
  },
  {
    "Date": "2021/Dec/01",
    "Printer id": "700",
    "Media category": "Film",
    "Printed square meters": 152.62
  },
  {
    "Date": "2021/Dec/01",
    "Printer id": "701",
    "Media category": "Heavy paper > 200gsm",
    "Printed square meters": 256.02000000000004
  },
  {
    "Date": "2021/Dec/01",
    "Printer id": "701",
    "Media category": "Textile",
    "Printed square meters": 144.95999999999998
  }, ...
]
// DESIRED OUTPUT
[
  {
    "Date": "2021/Dec/01",
    "Printer id": "700",
    "Canvas": 244.58,
    "Film": 152.62
  },
  {
    "Date": "2021/Dec/01",
    "Printer id": "701",
    "Heavy paper > 200gsm": 256.02000000000004,
    "Textile": 144.95999999999998
  }, ...

I tried doing the first one with this code, but got no idea how to transform the fields:我尝试用这段代码做第一个,但不知道如何转换字段:

// What I tried doing
console.log(_(data)
    .groupBy('Date')
    .map(g => _.mergeWith({}, ...g, (obj, src) =>
      _.isArray(obj) ? obj.concat(src) : undefined))
    .value());

Checked many questions before submitting this one but none has the the transformation of fields.在提交此之前检查了许多问题,但没有一个具有字段转换。

You can create a computed key on Date and Printer id and group based on this key.您可以基于此键在DatePrinter id和组上创建计算键。

 const data = [ { "Date": "2021/Dec/01", "Printer id": "700", "Media category": "Canvas", "Printed square meters": 244.58 }, { "Date": "2021/Dec/01", "Printer id": "700", "Media category": "Film", "Printed square meters": 152.62 }, { "Date": "2021/Dec/01", "Printer id": "701", "Media category": "Heavy paper > 200gsm", "Printed square meters": 256.02000000000004 }, { "Date": "2021/Dec/01", "Printer id": "701", "Media category": "Textile", "Printed square meters": 144.95999999999998 }], result = Object.values(data.reduce((r, o) => { let key = o.Date + '-' + o['Printer id']; r[key]??= {Date: o.Date, 'Printer id': o['Printer id']}; r[key][o['Media category']] = (r[key][o['Media category']]?? 0) + o['Printed square meters']; return r; },{})); console.log(result);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM