I want to map the response from service to different arrays. My response is of type
{
"success": true,
"dataPoints": [{
"count_id": 4,
"avg_temperature": 2817,
"startTime": "00:00:00",
"endTime": "00:19:59.999"
}, {
"count_id": 4,
"avg_temperature": 2814,
"startTime": "00:59:59.997",
"endTime": "01:19:59.996"
}, {
"count_id": 4,
"avg_temperature": 2816,
"startTime": "00:39:59.998",
"endTime": "00:59:59.997"
}, {
"count_id": 4,
"avg_temperature": 2825,
"startTime": "02:19:59.993",
"endTime": "02:39:59.992"
}, {
"count_id": 4,
"avg_temperature": 2828,
"startTime": "02:39:59.992",
"endTime": "02:59:59.991"
}, {
"count_id": 4,
"avg_temperature": 2832,
"startTime": "02:59:59.991",
"endTime": "03:19:59.99"
}, {
"count_id": 4,
"avg_temperature": 2841,
"startTime": "03:39:59.989",
"endTime": "03:59:59.988"
}, {
"count_id": 4,
"avg_temperature": 2816,
"startTime": "01:39:59.995",
"endTime": "01:59:59.994"
}, {
"count_id": 5,
"avg_temperature": 2668,
"startTime": "04:19:59.987",
"endTime": "04:39:59.986"
}, {
"count_id": 3,
"avg_temperature": 2711,
"startTime": "05:19:59.984",
"endTime": "05:39:59.983"
}]
I want to create an array of startTime , endTime, avg_temperature. I have read that it can be done by creating an interface of response type and map the fields. but i am not sure how to map. can someone help?
getHistoryData() {
this.historyDataService.getHistoryData(this.payload)
.subscribe(data => {this.responeData = data as IResponseData[];
console.log(this.responeData);
});
I don't see any difficult.
if you have your supposed data, it must works as you expected.
const response = {
"success": true,
"dataPoints": [{
"count_id": 4,
"avg_temperature": 2817,
"startTime": "00:00:00",
"endTime": "00:19:59.999"
}, {
"count_id": 4,
"avg_temperature": 2814,
"startTime": "00:59:59.997",
"endTime": "01:19:59.996"
}, {
"count_id": 4,
"avg_temperature": 2816,
"startTime": "00:39:59.998",
"endTime": "00:59:59.997"
}, {
"count_id": 4,
"avg_temperature": 2825,
"startTime": "02:19:59.993",
"endTime": "02:39:59.992"
}, {
"count_id": 4,
"avg_temperature": 2828,
"startTime": "02:39:59.992",
"endTime": "02:59:59.991"
}, {
"count_id": 4,
"avg_temperature": 2832,
"startTime": "02:59:59.991",
"endTime": "03:19:59.99"
}, {
"count_id": 4,
"avg_temperature": 2841,
"startTime": "03:39:59.989",
"endTime": "03:59:59.988"
}, {
"count_id": 4,
"avg_temperature": 2816,
"startTime": "01:39:59.995",
"endTime": "01:59:59.994"
}, {
"count_id": 5,
"avg_temperature": 2668,
"startTime": "04:19:59.987",
"endTime": "04:39:59.986"
}, {
"count_id": 3,
"avg_temperature": 2711,
"startTime": "05:19:59.984",
"endTime": "05:39:59.983"
}]
};
startTime = [];
avg_temperature = [];
endTime = [];
getHistoryData() {
this.startTime = [];
this.avg_temperature = [];
this.endTime = [];
this.historyDataService
.getHistoryData(this.payload)
.subscribe(
(data: any) =>
{
const responeData: any = data.dataPoints;
if(responeData == null){
return; // you don't have the expected data
}
startTime = responeData.map(e => e.startTime);
endTime = responeData.map(e => e.endTime);
avg_temperature = responeData.map(e => e.avg_temperature);
}
);
Declare 3 arrays to hold startime,endtime and avg_temperature;
startTime = [];
endTime = [];
avg_temperature = [];
getHistoryData() {
this.historyDataService.getHistoryData(this.payload)
.subscribe(data => {
this.responeData = data.dataPoints as IResponseData[];
this.responseData.forEach(x =>{
this.startTime.push(x.startTime),
this.endTime.push(x.endtime),
this.avg_temperature.push(x.avg_temperature)}
);
});
}
You seem to be looking for
getHistoryData() {
this.historyDataService.getHistoryData(this.payload).subscribe(response => {
const {dataPoints} = response;
const result = {
startTimes: dataPoints.map(p => p.startTime),
endTimes: dataPoints.map(p => p.endTime),
avgTemperatures: dataPoints.map(p => p.avg_temperature)
};
console.log(result);
});
}
See if this works for you.
const data = { "success": true, "dataPoints": [{ "count_id": 4, "avg_temperature": 2817, "startTime": "00:00:00", "endTime": "00:19:59.999" }, { "count_id": 4, "avg_temperature": 2814, "startTime": "00:59:59.997", "endTime": "01:19:59.996" }, { "count_id": 4, "avg_temperature": 2816, "startTime": "00:39:59.998", "endTime": "00:59:59.997" }, { "count_id": 4, "avg_temperature": 2825, "startTime": "02:19:59.993", "endTime": "02:39:59.992" }, { "count_id": 4, "avg_temperature": 2828, "startTime": "02:39:59.992", "endTime": "02:59:59.991" }, { "count_id": 4, "avg_temperature": 2832, "startTime": "02:59:59.991", "endTime": "03:19:59.99" }, { "count_id": 4, "avg_temperature": 2841, "startTime": "03:39:59.989", "endTime": "03:59:59.988" }, { "count_id": 4, "avg_temperature": 2816, "startTime": "01:39:59.995", "endTime": "01:59:59.994" }, { "count_id": 5, "avg_temperature": 2668, "startTime": "04:19:59.987", "endTime": "04:39:59.986" }, { "count_id": 3, "avg_temperature": 2711, "startTime": "05:19:59.984", "endTime": "05:39:59.983" }] }; const temp = data.dataPoints.map(obj => obj.avg_temperature); const startTime = data.dataPoints.map(obj => obj.startTime); const endTime = data.dataPoints.map(obj => obj.endTime); console.log(temp); console.log(startTime); console.log(endTime);
you can reduce the response to three separate arrays like this really simple:
// Helper function to map the object response to three arrays
mapResponse(response): DataVM {
return reponse && response.dataPoints ? response.dataPoints.reduce((total, curr) => {
return {
...total,
avgTemp: [...total.avgTemp, curr.avg_temperature],
endTime: [...total.startTime, curr.startTime],
startTime: [...total.endTime, curr.endTime]
}
}, {avgTemp: [], startTime: [], endTime: []}) : null;
}
// Map the response in pipe (Rx 5.5+) or .map (older RxJS versions)
getHistoryData() {
this.historyDataService.getHistoryData(this.payload)
.pipe(map(this.mapResponse))
.subscribe(data => {
console.log(data);
});
You can also create an interface to reflect your view model like:
export interface DataVM{
avgTemp: number[],
endTime: string[],
startTime: string[]
}
Good luck!
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.