Seeking assistance on how to filter out duplicate data. Currently receiving the following response:
{username:'patrick',userid:'3636363',position:'employee'}
{username:'patrick',userid:'3636363',position:'employee'}
{username:'patrick2',userid:'3636365',position:'manager'}
Goal is to display only:
{username:'patrick',userid:'3636363',position:'employee'}
{username:'patrick2',userid:'3636365',position:'manager'}
Below is a snippet of my component code:
onSubmit(data:string){
this.http.post(this.Url+'Employee?name='data.name,data).subscribe(res=>{
this.result=filterDuplicate(Object.values(res));
});
function filterDuplicate(users:any):any{
return users.filter(user,index)=>findIndex((u)=>user.username===u.username)===index);
}
}
html
<div *ngFor="let item of result |employee:'userId'">
{{item.userid}} {{item.username}}{{item.position}}
</div>
Attempted using:
this.result=Array.from(new Set(res.map(value => value.userid)));
but encountered an error property 'map' doesnot exist on type 'Object'
Also tried creating a pipe:
declare var _: any;
@Pipe({
name: 'employee',
pure: false
})
@Injectable()
export class EmployeePipe implements PipeTransform {
transform(value: any, args: any): any {
return _.uniqBy(value, args);
}
}
Encountering
Error ReferenceError: _ is not defined
while using the pipe.
Any assistance in listing distinct values will be greatly appreciated.res result:
>0:{username:'patrick',userid:'3636363',position:'employee'}
>1:{username:'patrick',userid:'3636363',position:'employee'}
>2:{username:'patrick2',userid:'3636365',position:'manager'}