Q:

How to parse data object from json in angular

I am just beginning with Angular 6.
I folllowed the Hero Tutorial, and then replaced it with a real local API
now, instead of heroes, I have tournaments
but my API instead of returning an array of tournaments, it wraps it into a data object, so I can add metadata.
tournament.service.ts
getTournaments(): Observable<Tournament[]> {
    this.messageService.add('TournamentService: fetched tournaments');
    return this.http.get<Tournament[]>(this.tournamentsUrl)
      .pipe(
        tap(tournaments => this.log(`fetched tournaments`)),
        catchError(this.handleError('getTournaments', []))
      );
  }

tournament.ts
export class Tournament {
  id: number;
  user: string;
  date: string;
  name: string;
  numCompetitors: number;
}

tournaments I get from API:
"data": [
{
"id": 1,
"user": "superuser@dev.com",
"date": "2018-06-13",
"name": "Fake Tournoi",
"numCompetitors": 86
},
{
"id": 2,
"user": "federation@dev.com",
"date": "2018-06-06",
"name": "Noel Kerluke",
"numCompetitors": 0
},
]

How should I do to always extract my array from data ?

A:

if tournaments is a object then, in component where you are subscribing the service method then you have to get that array of data like this tournaments["data"]
getTournaments(): void {
    this.tournamentService.getTournaments()
    .subscribe(tournaments => this.tournaments= tournaments["data"]);
  }

