Counting items in GroupBy by group.key in Angular with rxjs

Viewed 4528

I have a very large array of items where there is a "Status" field that could have one of six statuses. Using the rxjs library, I am trying to .groupBy then .reduce to return an array of the six statuses and the count of items with those statuses.

Here is the method in my service:

getStatusTotals() {
        return Observable.from(sampleData)
            .map(res => res.d.results)
            .concatMap(data => data)
            .groupBy(item => item.Status)
            .mergeMap(group => group
                .reduce((total, item) => total, 0)
                .map(total => ({ Status: group.key, Count: total }))
                )
            .toArray()

    }

In the component where I am calling this service function, I set the value to statusData and the information is being presented as follows:

<div *ngFor="let t of tracdata | async" class="row">Status: {{t.Status}}; Count: {{t.Count}}</div>

This, however is giving me the following in my app:

Status: 5 - Dormant; Count: 0
Status: 2 - Active; Count: 0
Status: 4 - Assigned to Section; Count: 0
Status: 1 - Pending Review; Count: 0
Status: 3 - Officer Review; Count: 0

So I am doing something wrong. I have tried .count and .forEach, and read through the rxjs documentation, but I must be doing something wrong. Please help!

2 Answers
Related