简体   繁体   English

使用Lodash将Javascript数组分组

[英]Group Javascript Array Using Lodash

Transaction.ts Transaction.ts

export class Transaction {
    id: string;
    category: Category = new Category();
    totalPrice: number;
}

Category .ts 类别.ts

export class Category {
    id: string;
    name: string;
}

I have an array of Transaction as shown below. 我有一个Transaction数组,如下所示。

id:"1"
category: {id: "1", name: "Plumber"}
totalPrice: 500

id:"2"
category: {id: "1", name: "Plumber"}
totalPrice: 500

id:"3"
category: {id: "2", name: "Electrician"}
totalPrice: 1000

Now I need to group that data as shown below. 现在,我需要对数据进行分组,如下所示。

在此处输入图片说明

I have tried to group it using lodash . 我试图用lodash将其分组 But unable to format the way which I need (as shown above). 但是无法格式化我需要的方式(如上所示)。 Can I have any support here? 我可以在这里得到任何支持吗?

This is what I tried. 这就是我尝试过的。 It just returned individual objects. 它只是返回了单个对象。 After that? 之后?

let myArray = groupBy(this.data.transactions, (n) => {
      return n.category.name;
    });

Like this (data is different here.Just dummy data) 像这样(这里的数据不同,只是伪数据)

在此处输入图片说明

Update: 更新:

 let a = groupBy(this.data.transactions, (n) => {
      return n.category.name;
    }).map((objs, key) => ({
      'name': key,
      'totalPrice': sumBy(objs, 'totalPrice')
    })).value();

Above gives this error: 上面给出了这个错误:

core.es5.js:1084 ERROR Error: Uncaught (in promise): TypeError: Object(...)(...).map is not a function TypeError: Object(...)(...).map is not a function core.es5.js:1084错误错误:未捕获(承诺):TypeError:Object(...)(...)。map不是函数TypeError:Object(...)(...)。map不是功能

Update 2: 更新2:

  const resultArray = [...reduce(this.data.transactions,(r, { totalPrice, category: { name } }) => {
      const item = r.get(name) || {
        name,
        totalPrice: 0
      };

      item.totalPrice += totalPrice;

      return r.set(name, item);
    }, new Map()).values()];

ERROR: 错误:

ERROR Error: Uncaught (in promise): TypeError: Object(...)(...).values(...).slice is not a function TypeError: Object(...)(...).values(...).slice is not a function 错误错误:未捕获(承诺):TypeError:Object(...)(...)。values(...)。slice不是函数TypeError:Object(...)(...)。values (...)。slice不是函数

You need groupBy and sumBy , 您需要groupBysumBy

var grouped =  _(jsonarray)
    .groupBy('category.name')
    .map((objs, key) => ({
        'name': key,
        'totalPrice': _.sumBy(objs, 'totalPrice') }))
    .value();

DEMO 演示

 var jsonarray = [{id:"1", category: {id: "1", name: "Plumber"}, totalPrice: 500 }, { id:"2", category: {id: "1", name: "Plumber"}, totalPrice: 500 }, { id:"3", category: {id: "2", name: "Electrician"}, totalPrice: 1000 }]; var grouped = _(jsonarray) .groupBy('category.name') .map((objs, key) => ({ 'name': key, 'totalPrice': _.sumBy(objs, 'totalPrice') })) .value(); console.log(grouped); 
 <script src='https://cdn.jsdelivr.net/lodash/4.17.2/lodash.min.js'></script> 

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

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