• Skip to main content
  • Skip to primary sidebar

Web Development Archive

  • Archive
You are here: Home / Other / Sort Pipe – Filter repetations

Sort Pipe – Filter repetations

Ismétlődések eltávolítása

<option *ngFor="let item of items$ | async | sortYears : 'year'">{{ item.year }}</option>
import { Pipe, PipeTransform } from '@angular/core';

@Pipe({
  name: 'sortYears',
  standalone: true,
})
export class SortYearsPipe implements PipeTransform {
  transform(array: any[] | null, field: string): any[] {
    if (!array) {
      return [];
    }

    // Először rendezzük az évszámokat.
    array.sort((a: any, b: any) => {
      if (a[field] < b[field]) {
        return -1;
      } else if (a[field] > b[field]) {
        return 1;
      } else {
        return 0;
      }
    });

    // Most eltávolítjuk az ismétlődéseket.
    return array.filter((value, index, self) => index === self.findIndex((t) => t[field] === value[field]));
  }
}

Filed Under: Other

About Gabor Flamich

I'm a web developer and designer based in Budapest, Hungary. In recent years, I've documented hundreds of solutions I came across during development. This site is an archive for useful code snippets on WordPress, Genesis Framework and WooCommerce. If You have any questions related to WordPress development, get in touch!

Primary Sidebar

  • angular.io
© 2026 WP Flames - All Right Reserved