Function with dynamic Arrays

Hello,
I am a noob in node red and I've wrote this function node and it works fine, but I have not every time three arrays, sometimes 2, sometimes up to 15 arrays and in this case my function node dosent work. I know there is a way to count the arrays, but how to ?????
Can somebody help me and change this code???

type or paste code here

var elv0 = msg.payload.data[0].elevation.feet;
var loc0 = msg.payload.data[0].station.location;
var typ0 = msg.payload.data[0].station.type;
var raw0 = msg.payload.data[0].raw_text;

var elv1 = msg.payload.data[1].elevation.feet;
var loc1 = msg.payload.data[1].station.location;
var typ1 = msg.payload.data[1].station.type;
var raw1 = msg.payload.data[1].raw_text;

var elv2 = msg.payload.data[2].elevation.feet;
var loc2 = msg.payload.data[2].station.location;
var typ2 = msg.payload.data[2].station.type;
var raw2 = msg.payload.data[2].raw_text;

msg.payload ='<li>'+loc0 + '  '+typ0+' - Elev: '+elv0+' ft'+'<br>'+raw0+'<br>'+
             '<li>'+loc1 + '  '+typ1+' - Elev: '+elv1+' ft'+'<br>'+raw1+'<br>'+
             '<li>'+loc2 + '  '+typ2+' - Elev: '+elv2+' ft'+'<br>'+raw2+'<br>';

return msg;

Try...

const data = msg.payload.data;
const result = data.map(e => {
    var elv = e.elevation.feet;
    var loc = e.station.location;
    var typ = e.station.type;
    var raw = e.raw_text;
    return `<li>${loc}  ${typ} - Elev: ${elv} ft<br>${raw}<br>`
})
msg.payload = result
return msg;
1 Like

wow.... it works

thank you very much, I've been trying for three days....

:slightly_smiling_face:

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.