I am new to Type script and trying to remove duplicate objects based on property in my case its only id. I am trying to use filter but unable to do so. I have pasted my code below . Could any one please guide me. MY list is of type employee and i have created model class for employee.id can be duplicate because of some bad data and as work around i need to remove duplicate from list and show it to the UI.

I have tried to convert it to set but didn't work.

     ids : Employee[] = new Array<Employee>();
     this.list.filter((this.list=> ids.includes(this.list.idPk) ? false : ids.push(this.list.idPk));

2 Answers

Community On
ids : Employee[] = new Array<Employee>();

 var newList  = this.list.filter(list => !ids.includes(list.idPk));
Jordan On

Sorry I couldn't get back to you as I was overloaded with current project work and put my learning at a side. Thank you for your answer i tried to do it but it gives me exception for semicolons even though i have put all semicolons. this list can have duplciate products having the same id. I need to remove duplicates based on Id from product.

   private productsList: Product[] = new Array<Product>();
   private prdList: Product[] = new Array<Product>();

   this.prdList  = this.productsList.filter(
    productsList => !this.prdList.includes(productsList.rowidObject));
   this.productsList = this.prdList;