0

Transaction.ts

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

Category .ts

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

I have an array of Transaction as shown below.

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.

enter image description here

I have tried to group it using 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)

enter image description here

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

Update 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

2
  • Possible duplicate of How to group data in Angular 2 ? Commented Oct 6, 2017 at 13:21
  • 1
    Hope I don't need to write pipe here. This is just a matter of choosing correct Lodash methods. Please remove the duplicate tag. @RameshRajendran Commented Oct 6, 2017 at 13:28

1 Answer 1

1

You need groupBy and sumBy,

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>

Sign up to request clarification or add additional context in comments.

9 Comments

Oh.. It's like a tsunami of Downvotes? I'm still trying to see your solution. But it is grayed :D
did you run the demo and see
Is this lodash method groupBy and map?
Here it 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
Can you see the Update section on my post?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.