After making an HTTP call, I received a JSON file with the following data:
[
{
"totalConfirmed": 555,
"mainlandChina": 548,
"otherLocations": 7,
"deltaConfirmed": 555,
"totalRecovered": 0,
"confirmed": {
"total": 555,
"china": 548,
"outsideChina": 7
},
"deaths": {
"total": 17,
"china": 17,
"outsideChina": 0
},
"reportDate": "2020-01-22"
},
{
"totalConfirmed": 654,
"mainlandChina": 643,
"otherLocations": 11,
"deltaConfirmed": 99,
"totalRecovered": 0,
"confirmed": {
"total": 654,
"china": 643,
"outsideChina": 11
},
"deaths": {
"total": 18,
"china": 18,
"outsideChina": 0
},
"reportDate": "2020-01-23"
}
]
My objective is to extract and store the values of
totalConfirmed, deaths, & reportDate
.
Therefore, the expected output should look like this:
{
totalConfirmed : [555, 654],
deaths: [17, 18],
reportDates: ["2020-01-22", "2020-01-23"]
}
Below is the function I implemented in my service.ts
:
public filteredData(): Observable<History> {
let dataHistory: History;
return this.httpClient.get(this.hostURL).pipe(
map(res => {
dataHistory.totalConfirmedPerDay.push(res["totalConfirmed"]);
dataHistory.totalDeathPerDay.push(res["deaths"]["total"]);
dataHistory.dates.push(res["reportDate"]);
return dataHistory;
})
);
}
Here is the interface structure I defined for the History data:
export interface History {
totalConfirmedPerDay: any[];
totalDeathPerDay: any[];
dates: any[any];
}
However, an error is occurring which says:
ERROR TypeError: Cannot read property 'totalConfirmedPerDay' of undefined