← All pipes
Collections

countBy

Summarize category frequencies without retaining grouped arrays.

Use it in your component

Standalone component
import { Component } from '@angular/core';
import { JsonPipe } from '@angular/common';
import { CountByPipe } from 'extra-pipe';

@Component({
  standalone: true,
  imports: [CountByPipe, JsonPipe],
  template: `{{ [{"team":"A"},{"team":"A"},{"team":"B"}] | countBy: "team" | json }}`,
})
export class ExampleComponent {

}

Template
{{ [{"team":"A"},{"team":"A"},{"team":"B"}] | countBy: "team" | json }}

Try it live.

The output below runs the packed library. Edit data, not code.

Use double quotes for text, or a number, array, object or null.
Ordered parameters: key. Locale is controlled separately.
This pipe has no locale override.
Live template data
readonly value = [
  {
    "team": "A"
  },
  {
    "team": "A"
  },
  {
    "team": "B"
  }
] as const;
readonly parameter0 = "team";
{{ value | countBy: parameter0 | json }}

Rendered output

[
  {
    "key": "A",
    "count": 2
  },
  {
    "key": "B",
    "count": 1
  }
]