I am learning javascript trying to incorporate some code that will go inside a Node Red function block. In the code below var data
is the json payload from an 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?
};
};
};
How could I incorporate the for loop for(var key in data){
into a function that returns supplyTemp
and returnTemp
if the keys
are found in the json payload? Something like return [[supplyTemp,returnTemp]];
Thanks for any javascript wisdom, this is something I am trying to gain...
You need value
properties of matching sensor1
and sensor2
to be stored in supplyTemp
and returnTemp
respectively? You can use filter
to filter the values that are matching the provided sensors, and then map the results to fetch values. Here's an example:
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])