[Fixed] Transform API response in Angular rxjs

Issue

Hi I’ve this response from API:

{
   "records":[
      {
         "id":1,
         "motivazione":"",
         "autorizzazione":false,

      }
   ]
}

How do I transform it like this:

[
      {
         "id":1,
         "motivazione":"",
         "autorizzazione":false,

      },
   ]

Thanks

Joseph

Solution

The question isn’t clear. If all you need is to get the contents of the property records, you could pipe a pluck operator to the HTTP request.

import { pluck } from 'rxjs/operators';

public someFunction(): Observable<any> {
  return this.http.get(url).pipe(
    pluck('records')
  );
}

Alternatively you could also use the map operator.

import { map } from 'rxjs/operators';

public someFunction(): Observable<any> {
  return this.http.get(url).pipe(
    map((res: any) => res['records'])
  );
}

Leave a Reply

(*) Required, Your email will not be published