0

My API returns this JSON, it's the same employee which is present 2 times in my database. I can't delete it in my database, also how can I print only 1 of them when I make my get in angular? Or when I make my *ngfor:"let e of employee", how can I print only the first result?

    getEmployee(id: string){
        return this.http.get<any>(this.localUrlAPI+"/employee/getEmployee/"+id)
        .pipe(map((res:any)=>{
          return res.Data;
        }))
      }
    {
      "Data":[
        {
        "IdEmployee": "1",
        "Name": "Jacob"
        }
        {
        "IdEmployee": "1",
        "Name" ; "Jacob"
        }
      ]
    }
Jason Aller
  • 3,541
  • 28
  • 38
  • 38
Kylian19
  • 19
  • 5
  • Does this answer your question? [Get all unique values in a JavaScript array (remove duplicates)](https://stackoverflow.com/questions/1960473/get-all-unique-values-in-a-javascript-array-remove-duplicates) – Crowcoder Feb 03 '22 at 13:06

1 Answers1

1

so you want to remove duplicates, there are many approaches, this is one:

.pipe(map((res:any)=>{
  const uniqueData = new Map<number, any>();
  for (const data of res.Data) {
    uniqueData.set(data.id, data);
  }
  return  Array.from(uniqueData.values());
}))
Zerotwelve
  • 2,087
  • 1
  • 9
  • 24