Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

411
Vistas
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 la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda