项目场景:
鄂尔多斯项目问题描述:
APP 中接收数据代码: [
{
"Identifier": "Temperature",
"Value": "24.0",
"DataType": "float",
"Time": "1623756218857",
"Unit": "°C",
"Name": "环境温度"
}
{
"Identifier": "WindTar",
"Value": "4",
"DataType": "enum",
"Time": "1623756218857",
"Unit": "",
"Name": "风向"
}
]
处理逻辑:
// 思路:
// 1.提取所需对象
// 2.switc循环重新赋值
var arr = []
for (var key in data) {
if (data[key].Identifier == 'WindTar') {
arr.push(data[key])
}
}
switch (arr[0].Value) {
case '0':
arr[0].Value = "北风"
break;
case '1':
arr[0].Value = "东北风"
break;
case '2':
arr[0].Value = "东风"
break;
case '3':
arr[0].Value = "东南风"
break;
case '4':
arr[0].Value = "南风"
break;
case '5':
arr[0].Value = "西南风"
break;
case '6':
arr[0].Value = "西风"
break;
case '7':
arr[0].Value = "西北风"
break;
default:
arr[0].Value = "风向数据错误"
}
return arr
最终结果:
[
{
"Identifier": "WindTar",
"Value": "南风",
"DataType": "enum",
"Time": "1623756218857",
"Unit": "",
"Name": "风向"
}
]
法二:
源数据:
{
"WindDirection": {
"value": 0,
"timestamp": 1638951875069
},
"Atmosphere": {
"value": 102.60000000000001,
"timestamp": 1638951875069
}
}
处理逻辑:
// 思路:
// 1.提取所需对象
// 2.if条件判断重新赋值
var WindDirection = Math.ceil(data.WindDirection.value);
var Atmosphere = Math.ceil(data.Atmosphere.value);
if (WindDirection == 0) {
WindDirection = "北"
} else if (WindDirection == 1) {
WindDirection = "东北"
} else if (WindDirection == 2) {
WindDirection = "东"
} else if (WindDirection == 3) {
WindDirection = "东南"
} else if (WindDirection == 4) {
WindDirection = "南"
} else if (WindDirection == 5) {
WindDirection = "西南"
} else if (WindDirection == 6) {
WindDirection = "西"
} else if (WindDirection == 7) {
WindDirection = "西北"
}
var obj = {
"温度": Temperature + "℃",
"湿度": Humidity + "%",
"风速": WindSpeed + "km/h",
"风向": WindDirection,
"光照": Lux + "lux",
"气压": Atmosphere + "hPa"
}
return [obj];
最终结果:
[
{
"温度": "10℃",
"湿度": "42%",
"风速": "0km/h",
"风向": "北",
"光照": "129lux",
"气压": "103hPa"
}
]