← All pipes
Collections

indexBy

Build a prototype-safe lookup Map from records.

Use it in your component

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

@Component({
  standalone: true,
  imports: [IndexByPipe, JsonPipe, KeyValuePipe],
  template: `{{ [{"id":1,"name":"Ana"},{"id":2,"name":"Sam"}] | indexBy: "id" | keyvalue: keepInsertionOrder | json }}`,
})
export class ExampleComponent {
readonly keepInsertionOrder = () => 0;
}

Template
{{ [{"id":1,"name":"Ana"},{"id":2,"name":"Sam"}] | indexBy: "id" | keyvalue: keepInsertionOrder | 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 keepInsertionOrder = () => 0;
readonly value = [
  {
    "id": 1,
    "name": "Ana"
  },
  {
    "id": 2,
    "name": "Sam"
  }
] as const;
readonly parameter0 = "id";
{{ value | indexBy: parameter0 | keyvalue: keepInsertionOrder | json }}

Rendered output

[
  [
    1,
    {
      "id": 1,
      "name": "Ana"
    }
  ],
  [
    2,
    {
      "id": 2,
      "name": "Sam"
    }
  ]
]