Estoy trabajando con Angular dos con el siguiente 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"
}
]
}
Para registrar esta respuesta en mi front he diseñado esto:
Una clase encargada del modelo de datos con los tres atributos.
En mi component he declarado este dato:
miData: any = {};
Asignando la respuesta de tal forma:
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;
Mostrando la información por ejemplo:
console.log('mostramos primer log: ' + var_CO2Exterior);
Obtengo este resultado (un objeto con la información)
mostramos primer log: [object Object]
Utiliando:
console.log('mostramos primer log: ' + JSON.stringify(var_CO2Exterior));
Se muestra lo que contiene ese objeto:
mostramos primer log: [{"attrName":"var_CO2Exterior","attrValue":"8","recvTime":"2018-02-02T13:57:45.000Z"}]
¿Como puedo extraer la información de cada variable para no tener un objeto? antes lo hacia con un for y anidando if comprobando el nombre pero est hace muy lenta la carga.
if (respuesta.some(inv => inv.attrName=='var_CO2Exterior')) {
let valores= respuesta.map(inv => inv.attrValue);
console.log(valores);
}
Para poder mostrar en el html {{var_CO2Exterior.value}}
Un saludo y gracias.