I have a model set up for the data I am retrieving through a GET request, and my goal is to map this data to an array of Player
objects.
This is an example of the response data received from the API:
[
{
Id: 1
PlayerName: "Dave",
FirstSeason: 2013,
LastSeason: 2016,
BattingAverageRecord: [340, 210, 220, 300]
},
{
Id: 2
PlayerName: "Dennis",
FirstSeason: 2013,
LastSeason: 2016,
BattingAverageRecord: [230, 221, 312, 240]
},
{
Id: 3
PlayerName: "Mary",
FirstSeason: 2010,
LastSeason: 2013,
BattingAverageRecord: [330, 123, 151, 307]
}
];
A model has been established for this data:
export class Player {
constructor(public Id: number,
public PlayerName: string,
public FirstSeason: number,
public LastSeason: number,
public BattingAverageRecord: number[])
{
this.Id = Id;
this.PlayerName = PlayerName;
this.FirstSeason = FirstSeason;
this.LastSeason = LastSeason;
this.BattingAverageRecord = BattingAverageRecord;
}
}
Now, my objective is to link the response data to an array of Players
:
retrievePlayer(): Observable<any> {
return this.http.get<Player[]>(this.playerAPI)
.pipe(
map(data => {
return new Player(data.Id, data.PlayerName, data.FirstSeason, data.LastSeason, data.GPARecord))
}
)
}
I tried using
new Array(new Player(data.id ...))
, but I know that's incorrect too.