Angular's pipe filter not returning distinct values

1.5k Views Asked by At

I have an application that uses the same service data on two different pages i.e Parent and Child.

On one of the pages, I am trying to filter the data to return a unique array based on a column using ngx-filter-pipe module but the data still returns all values.

How do I return distinct values?

My code:

.module.ts-

import { NgModule }      from '@angular/core';
import { NgPipesModule } from 'ngx-pipes';
@NgModule({
   imports: [
       NgPipesModule
     ]
})
...

.component.html-

<div padding=true>
  <ul>
    <li *ngFor="let group of musicList| unique: PLAYLIST_GROUP"></li>
  </ul>
</div>

data from service-

[
 {
   "KEYS": 1,
   "ARTIST": "Jamila Woods",
   "TITLE": "LSD",
   "ALBUM": "HEAVN",
   "PLAYLIST_GROUP": "RnB/Soul",
   "COUNT": 3
 },
 {
   "KEYS": 2,
   "ARTIST": "Travis Scott",
   "TITLE": "SICKO MODE",
   "ALBUM": "ASTROWORLD",
   "PLAYLIST_GROUP": "Hip Hop",
   "COUNT": 1
 },
 {
   "KEYS": 3,
   "ARTIST": "Rihanna",
   "TITLE": "ANTI",
   "ALBUM": "Yeah, I Said it",
   "PLAYLIST_GROUP": "RnB/Soul",
   "COUNT": 3
 },
 {
   "KEYS": 4,
   "ARTIST": "Summer Walker",
   "TITLE": "Girls Need Love",
   "ALBUM": "Last Day of Summer",
   "PLAYLIST_GROUP": "RnB/Soul",
   "COUNT": 3
 }
]
3

There are 3 best solutions below

0
On BEST ANSWER

According to the documentation for unique. The option requires another set of quotes.

Usage: array | unique: 'Property (Optional)'

<div padding=true>
  <ul>
    <li *ngFor="let group of musicList| unique: 'PLAYLIST_GROUP'"></li> <!--Quotes-->
  </ul>
</div>
2
On

Try by putting the property name inside single quotes.

1
On

Try this,

<li *ngFor="let group of musicList | unique: 'group.PLAYLIST_GROUP' "></li>

Or,

<li *ngFor="let group of (musicList | unique: 'group.PLAYLIST_GROUP') "></li>

Reference, As follows in the documentation https://angular.io/guide/pipes

https://www.npmjs.com/package/ngx-pipes#unique