Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

154
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda