I am trying to implement pipes using Angular. Below is the code I have tried. I want to retrieve unique for the complete list . So i have added a pipe filter name for the inner list . But i am still getting the duplicate elements. I have added the json for reference .The inner ArticleTags array has a list of objects. Similarly I have multiple ArticleTags array for every parent Array. I want to retrieve the unique elements from the entire list ArticleTags array. I think its retrieving the unique elements within the particular inner list and not retrieving from the entire list of Article Tags.
import { Pipe, PipeTransform } from '@angular/core';
@Pipe({
name: 'filterUnique',
pure: false
})
export class FilterPipe implements PipeTransform {
transform(value: any, args?: any): any {
// Remove the duplicate elements
const uniqueArray = value.filter(function (el, index, array) {
return array.indexOf (el) === index;
});
return uniqueArray;
}
}
<ul>
<li *ngFor="let articlesResult of articlesListArray; let i=index">
<ul>
<li *ngFor="let articlesTagResult of articlesResult.ArticleTags | filterUnique; let j=index">
<i class="fa fa-times-circle" *ngIf="articlesResult.ArticleTags[j].value"></i>
<label class="form-check-label" for="exampleCheck" *ngIf="articlesResult.ArticleTags[j].value">{{articlesResult.ArticleTags[j].value}}</label>
</li>
</ul>
</li>
</ul>
getLatestArticles(currdate): void {
this.ng4LoadingSpinnerService.show();
this.ArticlesServiceCall.getArticlesDashboard(currdate)
.subscribe(
resultArray => {
this.ng4LoadingSpinnerService.hide();
this.articlesList = resultArray;
this.articlesLists = resultArray.ResponseValue;
this.articlesListArray = this.articlesLists.slice(0, 8);
},
error => console.log('Error :: ' + error)
);
}
I am getting the main array data from articlesListArray and passing that in html
Edit update on July 09 2018
Getting the below error with the below pipe code.
import { Pipe, PipeTransform } from '@angular/core';
@Pipe({ name: 'filterduplicates' }) export class FilterduplicatesPipe implements PipeTransform {
transform(value: any, args?: any): any {
// Remove the duplicate elements
const art = value.map( x => {
return x.ArticleTags ? x.ArticleTags.map(y => {
return y.value ? y.value : null;
}) : [];
}).reduce((acc, ele, i) => {
acc = acc.concat(ele);
return acc;
}).filter( z => {
if (z) {
return z;
}
});
return new Set(art);
}
articlesListArray
to getarticlesResult
, then in second<li>
tag you are looping througharticlesResult.ArticleTags
to get uniquearticlesTagResult
, but instead of usingarticlesTagResult.value
, you are usingarticlesResult.ArticleTags[j].value
? – j4rey