Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

155
Views
javascript loop through API response json data

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...

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

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])

about 4 years ago · Juan Pablo Isaza Report

0

Create a method which returns the value of the object you find. My code is very similar to @deepak except it's D.R.Y. (Don't repeat yourself).

I opted for .find() over .filter() as your code suggests the sensors are unique.

  • Find will either; return the matching item or null.
  • Filter will return an array of matching items or an empty array.

You should choose whichever one you prefer.

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;
}
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!