javascript 循环 API 响应 json 数据
javascript loop through API response json data
我正在学习 javascript 尝试合并一些将进入 Node Red 功能块的代码。在下面的代码中,var data
是来自 API.
的 json 有效负载
var data = {
"201201/tempUoTwoBalco":
{"value": 63.1199951171875, "route": "/vui/platforms/benshome/devices/201201/tempUoTwoBalco", "writable": false},
"201201/tempUoThreeBalco":
{"value": 74.12999725341797, "route": "/vui/platforms/benshome/devices/201201/tempUoThreeBalco", "writable": false},
"201201/Oat":
{"value": 89.99999237060547, "route": "/vui/platforms/benshome/devices/201201/Oat", "writable": false},
"201201/RmTmpSpt":
{"value": 79.99999237060547, "route": "/vui/platforms/benshome/devices/201201/RmTmpSpt", "writable": true},
"201201/RmTmp":
{"value": NaN, "route": "/vui/platforms/benshome/devices/201201/RmTmp", "writable": false},
"201201/UhCmd":
{"value": 0, "route": "/vui/platforms/benshome/devices/201201/UhCmd", "writable": false},
"201201/GlblHtgDsbl":
{"value": 0, "route": "/vui/platforms/benshome/devices/201201/GlblHtgDsbl", "writable": false}
};
console.log(data)
var supplyTemp = {};
var returnTemp = {};
sensor1 = "/vui/platforms/benshome/devices/201201/tempUoTwoBalco"
sensor2 = "/vui/platforms/benshome/devices/201201/tempUoThreeBalco"
for(var key in data){
for(var key1 in data[key]){
if(key1 === sensor1){
console.log("sensor1 found "+ data[key][key1])
// append float to supplyTemp?
};
if(key1 === sensor2){
console.log("sensor2 found "+ data[key][key1])
// append float to returnTemp?
};
};
};
如果 keys
在 json有效载荷?类似于 return [[supplyTemp,returnTemp]];
感谢任何 javascript 智慧,这是我想要获得的东西...
您需要 value
匹配 sensor1
和 sensor2
的属性分别存储在 supplyTemp
和 returnTemp
中?您可以使用 filter
过滤与提供的传感器匹配的值,然后将结果映射到获取值。这是一个例子:
var data = {
"201201/tempUoTwoBalco":
{ "value": 63.1199951171875, "route": "/vui/platforms/benshome/devices/201201/tempUoTwoBalco", "writable": false },
"201201/tempUoThreeBalco":
{ "value": 74.12999725341797, "route": "/vui/platforms/benshome/devices/201201/tempUoThreeBalco", "writable": false },
"201201/Oat":
{ "value": 89.99999237060547, "route": "/vui/platforms/benshome/devices/201201/Oat", "writable": false },
"201201/RmTmpSpt":
{ "value": 79.99999237060547, "route": "/vui/platforms/benshome/devices/201201/RmTmpSpt", "writable": true },
"201201/RmTmp":
{ "value": NaN, "route": "/vui/platforms/benshome/devices/201201/RmTmp", "writable": false },
"201201/UhCmd":
{ "value": 0, "route": "/vui/platforms/benshome/devices/201201/UhCmd", "writable": false },
"201201/GlblHtgDsbl":
{ "value": 0, "route": "/vui/platforms/benshome/devices/201201/GlblHtgDsbl", "writable": false }
};
var supplyTemp = {};
var returnTemp = {};
let sensor1 = "/vui/platforms/benshome/devices/201201/tempUoTwoBalco"
let sensor2 = "/vui/platforms/benshome/devices/201201/tempUoThreeBalco"
supplyTemp = (Object.values(data).filter(element => element.route == sensor1).map(result => result.value))
returnTemp = (Object.values(data).filter(element => element.route == sensor2).map(result => result.value))
console.log([supplyTemp, returnTemp])
创建一个方法,其中 return 是您找到的对象的值。
我的代码与@deepak 非常相似,除了它是 D.R.Y。 (不要重复自己)。
我选择 .find()
而不是 .filter()
,因为您的代码表明传感器是独一无二的。
- 查找将要么; return 匹配项或为空。
- 过滤器将 return 一个匹配项数组或一个空数组。
你可以选择你喜欢的那个。
let data = {
"201201/tempUoTwoBalco":
{"value": 63.1199951171875, "route": "/vui/platforms/benshome/devices/201201/tempUoTwoBalco", "writable": false},
"201201/tempUoThreeBalco":
{"value": 74.12999725341797, "route": "/vui/platforms/benshome/devices/201201/tempUoThreeBalco", "writable": false},
"201201/Oat":
{"value": 89.99999237060547, "route": "/vui/platforms/benshome/devices/201201/Oat", "writable": false},
"201201/RmTmpSpt":
{"value": 79.99999237060547, "route": "/vui/platforms/benshome/devices/201201/RmTmpSpt", "writable": true},
"201201/RmTmp":
{"value": NaN, "route": "/vui/platforms/benshome/devices/201201/RmTmp", "writable": false},
"201201/UhCmd":
{"value": 0, "route": "/vui/platforms/benshome/devices/201201/UhCmd", "writable": false},
"201201/GlblHtgDsbl":
{"value": 0, "route": "/vui/platforms/benshome/devices/201201/GlblHtgDsbl", "writable": false}
};
let supplyTemp = {};
let returnTemp = {};
let sensor1 = "/vui/platforms/benshome/devices/201201/tempUoTwoBalco";
let sensor2 = "/vui/platforms/benshome/devices/201201/tempUoThreeBalco";
console.log(getTemp(sensor1, data));
// returns 63.1199951171875
console.log(getTemp(sensor1, data));
// returns 63.1199951171875
console.log(getTemp('', data));
// returns null
/**
* Get the sensor value for the given sensorRoute in the given array.
*/
function getTemp(sensorRoute, arr) {
let foundItem = Object.values(arr).find(element => element.route == sensorRoute);
return foundItem ? foundItem.value : null;
}
我正在学习 javascript 尝试合并一些将进入 Node Red 功能块的代码。在下面的代码中,var data
是来自 API.
var data = {
"201201/tempUoTwoBalco":
{"value": 63.1199951171875, "route": "/vui/platforms/benshome/devices/201201/tempUoTwoBalco", "writable": false},
"201201/tempUoThreeBalco":
{"value": 74.12999725341797, "route": "/vui/platforms/benshome/devices/201201/tempUoThreeBalco", "writable": false},
"201201/Oat":
{"value": 89.99999237060547, "route": "/vui/platforms/benshome/devices/201201/Oat", "writable": false},
"201201/RmTmpSpt":
{"value": 79.99999237060547, "route": "/vui/platforms/benshome/devices/201201/RmTmpSpt", "writable": true},
"201201/RmTmp":
{"value": NaN, "route": "/vui/platforms/benshome/devices/201201/RmTmp", "writable": false},
"201201/UhCmd":
{"value": 0, "route": "/vui/platforms/benshome/devices/201201/UhCmd", "writable": false},
"201201/GlblHtgDsbl":
{"value": 0, "route": "/vui/platforms/benshome/devices/201201/GlblHtgDsbl", "writable": false}
};
console.log(data)
var supplyTemp = {};
var returnTemp = {};
sensor1 = "/vui/platforms/benshome/devices/201201/tempUoTwoBalco"
sensor2 = "/vui/platforms/benshome/devices/201201/tempUoThreeBalco"
for(var key in data){
for(var key1 in data[key]){
if(key1 === sensor1){
console.log("sensor1 found "+ data[key][key1])
// append float to supplyTemp?
};
if(key1 === sensor2){
console.log("sensor2 found "+ data[key][key1])
// append float to returnTemp?
};
};
};
如果 keys
在 json有效载荷?类似于 return [[supplyTemp,returnTemp]];
感谢任何 javascript 智慧,这是我想要获得的东西...
您需要 value
匹配 sensor1
和 sensor2
的属性分别存储在 supplyTemp
和 returnTemp
中?您可以使用 filter
过滤与提供的传感器匹配的值,然后将结果映射到获取值。这是一个例子:
var data = {
"201201/tempUoTwoBalco":
{ "value": 63.1199951171875, "route": "/vui/platforms/benshome/devices/201201/tempUoTwoBalco", "writable": false },
"201201/tempUoThreeBalco":
{ "value": 74.12999725341797, "route": "/vui/platforms/benshome/devices/201201/tempUoThreeBalco", "writable": false },
"201201/Oat":
{ "value": 89.99999237060547, "route": "/vui/platforms/benshome/devices/201201/Oat", "writable": false },
"201201/RmTmpSpt":
{ "value": 79.99999237060547, "route": "/vui/platforms/benshome/devices/201201/RmTmpSpt", "writable": true },
"201201/RmTmp":
{ "value": NaN, "route": "/vui/platforms/benshome/devices/201201/RmTmp", "writable": false },
"201201/UhCmd":
{ "value": 0, "route": "/vui/platforms/benshome/devices/201201/UhCmd", "writable": false },
"201201/GlblHtgDsbl":
{ "value": 0, "route": "/vui/platforms/benshome/devices/201201/GlblHtgDsbl", "writable": false }
};
var supplyTemp = {};
var returnTemp = {};
let sensor1 = "/vui/platforms/benshome/devices/201201/tempUoTwoBalco"
let sensor2 = "/vui/platforms/benshome/devices/201201/tempUoThreeBalco"
supplyTemp = (Object.values(data).filter(element => element.route == sensor1).map(result => result.value))
returnTemp = (Object.values(data).filter(element => element.route == sensor2).map(result => result.value))
console.log([supplyTemp, returnTemp])
创建一个方法,其中 return 是您找到的对象的值。 我的代码与@deepak 非常相似,除了它是 D.R.Y。 (不要重复自己)。
我选择 .find()
而不是 .filter()
,因为您的代码表明传感器是独一无二的。
- 查找将要么; return 匹配项或为空。
- 过滤器将 return 一个匹配项数组或一个空数组。
你可以选择你喜欢的那个。
let data = {
"201201/tempUoTwoBalco":
{"value": 63.1199951171875, "route": "/vui/platforms/benshome/devices/201201/tempUoTwoBalco", "writable": false},
"201201/tempUoThreeBalco":
{"value": 74.12999725341797, "route": "/vui/platforms/benshome/devices/201201/tempUoThreeBalco", "writable": false},
"201201/Oat":
{"value": 89.99999237060547, "route": "/vui/platforms/benshome/devices/201201/Oat", "writable": false},
"201201/RmTmpSpt":
{"value": 79.99999237060547, "route": "/vui/platforms/benshome/devices/201201/RmTmpSpt", "writable": true},
"201201/RmTmp":
{"value": NaN, "route": "/vui/platforms/benshome/devices/201201/RmTmp", "writable": false},
"201201/UhCmd":
{"value": 0, "route": "/vui/platforms/benshome/devices/201201/UhCmd", "writable": false},
"201201/GlblHtgDsbl":
{"value": 0, "route": "/vui/platforms/benshome/devices/201201/GlblHtgDsbl", "writable": false}
};
let supplyTemp = {};
let returnTemp = {};
let sensor1 = "/vui/platforms/benshome/devices/201201/tempUoTwoBalco";
let sensor2 = "/vui/platforms/benshome/devices/201201/tempUoThreeBalco";
console.log(getTemp(sensor1, data));
// returns 63.1199951171875
console.log(getTemp(sensor1, data));
// returns 63.1199951171875
console.log(getTemp('', data));
// returns null
/**
* Get the sensor value for the given sensorRoute in the given array.
*/
function getTemp(sensorRoute, arr) {
let foundItem = Object.values(arr).find(element => element.route == sensorRoute);
return foundItem ? foundItem.value : null;
}