I'm working with Angular two with the following JSON:
{
"var_TempExt": [
{
"attrName": "var_TempExt",
"attrValue": "8",
"recvTime": "2018-02-02T13:57:45.000Z"
},
{
"attrName": "var_TempExt",
"attrValue": "8",
"recvTime": "2018-02-02T13:57:45.000Z"
},
{
"attrName": "var_TempExt",
"attrValue": "8",
"recvTime": "2018-02-02T13:57:45.000Z"
},
{
"attrName": "var_TempExt",
"attrValue": "8",
"recvTime": "2018-02-02T13:57:45.000Z"
}
],
"var_TempIxt": [
{
"attrName": "var_TempInt",
"attrValue": "15",
"recvTime": "2018-02-02T13:57:45.000Z"
},
{
"attrName": "var_TempInt",
"attrValue": "15",
"recvTime": "2018-02-02T13:57:45.000Z"
},
{
"attrName": "var_TempInt",
"attrValue": "15",
"recvTime": "2018-02-02T13:57:45.000Z"
},
{
"attrName": "var_TempInt",
"attrValue": "15",
"recvTime": "2018-02-02T13:57:45.000Z"
}
],
"var_HumRExt": [
{
"attrName": "var_HumRExt",
"attrValue": "8",
"recvTime": "2018-02-02T13:57:45.000Z"
}
],
"var_HumRInt": [
{
"attrName": "var_HumRInt",
"attrValue": "8",
"recvTime": "2018-02-02T13:57:45.000Z"
}
],
"var_RadiacionExt": [
{
"attrName": "var_RadiacionExt",
"attrValue": "8",
"recvTime": "2018-02-02T13:57:45.000Z"
}
],
"var_RadiacionGlobalInt": [
{
"attrName": "var_RadiacionGlobalInt",
"attrValue": "8",
"recvTime": "2018-02-02T13:57:45.000Z"
}
],
"var_CO2Exterior": [
{
"attrName": "var_CO2Exterior",
"attrValue": "8",
"recvTime": "2018-02-02T13:57:45.000Z"
}
]
}
To register this response on my front I designed this:
A class responsible for the data model with the three attributes.
In my component I have declared this information:
miData: any = {};
Assigning the answer in such a way:
this.miData = response;
let var_TempExt: Inver[] = this.miData.var_TempExt;
let var_TempInt: Inver[] = this.miData.var_TempInt;
let var_HumRExt: Inver[] = this.miData.var_HumRExt;
let var_HumRInt: Inver[] = this.miData.var_HumRInt;
let var_RadiacionExt: Inver[] = this.miData.var_RadiacionExt;
let var_RadiacionGlobalInt: Inver[] = this.miData.var_RadiacionGlobalInt;
let var_CO2Exterior: Inver[] = this.miData.var_CO2Exterior;
Showing the information for example:
console.log('mostramos primer log: ' + var_CO2Exterior);
I get this result (an object with the information)
mostramos primer log: [object Object]
Using:
console.log('mostramos primer log: ' + JSON.stringify(var_CO2Exterior));
It shows what that object contains:
mostramos primer log: [{"attrName":"var_CO2Exterior","attrValue":"8","recvTime":"2018-02-02T13:57:45.000Z"}]
How can I extract the information of each variable so as not to have an object? before I did it with a for and nesting if checking the name but it is very slow loading.
if (respuesta.some(inv => inv.attrName=='var_CO2Exterior')) {
let valores= respuesta.map(inv => inv.attrValue);
console.log(valores);
}
To be able to show on the html {{var_CO2Exterior.value}}
Greetings and thanks.