Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
178
rated 0 times [  181] [ 3]  / answers: 1 / hits: 21094  / 7 Years ago, thu, april 20, 2017, 12:00:00

How to get unique records from this array. I need to get unique {{ subitem.author }} from this array of items.



<div *ngFor=let item of items>   
<ion-list *ngFor=let subitem of item.items (click)=authorquotes(subitem.author);>
<ion-item >
{{ subitem.author }}
</ion-item>
</ion-list>
</div>


In array having multiple records. From that array, I need to filter unique authors.


More From » angular

 Answers
5

You have to create a pipe that filters the array with unique items:



@Pipe({
name: 'filterUnique',
pure: false
})
export class FilterPipe implements PipeTransform {

transform(value: any, args?: any): any {

// Remove the duplicate elements
let uniqueArray = value.filter(function (el, index, array) {
return array.indexOf (el) == index;
});

return uniqueArray;
}
}


Then you can apply your pipe:



<div *ngFor=let item of items | filterUnique>   
<ion-list *ngFor=let subitem of item.items (click)=authorquotes(subitem.author);>
<ion-item >
{{ subitem.author }}
</ion-item>
</ion-list>
</div>


Working demo: https://plnkr.co/edit/yxvoKVD3Nvgz0T3AB7w3?p=preview


[#58085] Tuesday, April 18, 2017, 7 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
cadendericki

Total Points: 482
Total Questions: 109
Total Answers: 103

Location: Ecuador
Member since Thu, Jun 4, 2020
4 Years ago
cadendericki questions
Wed, Apr 7, 21, 00:00, 3 Years ago
Wed, Jul 8, 20, 00:00, 4 Years ago
Thu, May 14, 20, 00:00, 4 Years ago
;