← All pipes
Collections

groupBy

Readonly object arrays become ordered `{key, items}` groups.

Use it in your component

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

@Component({
  standalone: true,
  imports: [GroupByPipe, JsonPipe],
  template: `{{ items | groupBy: 'team' | json }}`,
})
export class ExampleComponent {
readonly items = [{team:'A',name:'Ana'},{team:'B',name:'Sam'},{team:'A',name:'Lee'}];
}

Template
{{ items | groupBy: '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",
    "name": "Ana"
  },
  {
    "team": "B",
    "name": "Sam"
  },
  {
    "team": "A",
    "name": "Lee"
  }
] as const;
readonly parameter0 = "team";
{{ value | groupBy: parameter0 | json }}

Rendered output

[
  {
    "key": "A",
    "items": [
      {
        "team": "A",
        "name": "Ana"
      },
      {
        "team": "A",
        "name": "Lee"
      }
    ]
  },
  {
    "key": "B",
    "items": [
      {
        "team": "B",
        "name": "Sam"
      }
    ]
  }
]