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

410
Visualizações
Issues passing 2D array in NodeRed

I am using node-red (JS) to take data from a sensor transmitting over MQTT. Sensor data is parsed as an array of 4 values [a, b, c, d] relating to 4 different measurements.

This data is stored into a global array named dataArray, which is a 2 dimensional array.

dataArray[0] = [a, b, c, d];

so that

dataArray[i][0] = Temperature
dataArray[i][1] = VibrationX
dataArray[i][2] = VibrationY
dataArray[i][3] = VibrationZ

The standard way i'd access the contents of this array would be

dataArray[i][j]

However, I get the error "Cannot access property of undefined" when attempting to read the array to calculate averages.

Ok, so i removed the second dimension, and tried accessing dataArray[0] - This returned 'array[0]' an empty array.

Then i attempted to just return the dataArray as passed to the function: i.e.

function average(data)
{
    return data;
}

average(dataArray);

this also returned array[0].

Finally I manaully specified the array within the funtion

dataArray = [[a, b, c, d], [a1, b1, c1, d1]];

and this returned the expected result.

I think it has something to do with the way the array is passed to the definition, or the way that the object is stored in global before access, but no amount of googling has helped me figure this out.

code:

    function Average(data) {
        var temperatureValue = 0;
        var vibXValue = 0;
        var vibYValue = 0;
        var vibZValue = 0;
        for (var i = 0, n = data.length; i < n; i++) {
            temperatureValue = data[i][0] + temperatureValue;
            vibXValue = data[i][1] + vibXValue;
            vibYValue = data[i][2] + vibYValue;
            vibZValue = data[i][3] + vibZValue;
        }
        //Average values from each array element
        var msg1 = [];
        //msg1 = [temperatureValue / n, vibXValue / n, vibYValue / n, vibZValue / n]
    
        //DEBUG - Send dataArray straight through, and it's length
        msg1 = [data, data.length];
        return msg1;
    } 



var dataArray = global.get("dataArray")||[];
var sensorData = msg.payload;

 //This is the input from the Cron node. As long as the input to the function isn't "ClearArray" then push new MQTT messages to the dataArray.
if (msg.payload != "ClearArray") {        
        dataArray.push(sensorData);
}

else if (dataArray.length != 0) {
    msg1 = Average(dataArray);
    msg2 = Peak(dataArray);
    msg.payload = [msg1, msg2];
    //global.set("dataArray",[]);
    return msg;
}
about 4 years ago · Juan Pablo Isaza
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