Looping an array of multiple objects into one payload

416 views
Skip to first unread message

Mike Arney

unread,
Mar 13, 2018, 3:08:32 PM3/13/18
to Node-RED
My OS got corrupted somehow, and I forgot to backup my reports tab in NodeRed. I had an automated email that would send me one message with 3 separate array objects. The email looked like this:

Shift: 1
DateRan: Mon Feb 19 2018 15:00:04 GMT-0500 (EST)
Efficiency: 60.20

Shift: 3
DateRan: Mon Feb 19 2018 07:00:07 GMT-0500 (EST)
Efficiency: 51.28

Shift: 2
DateRan: Sun Feb 18 2018 23:00:05 GMT-0500 (EST)
Efficiency: 36.47

I have recreated the query and email portion, but I can not format my email to reflect the example above.

This is what I am getting in my debug node:

I know that I used a function node to do something like this (but not exactly):


s = msg.payload[0].Shift;
d = msg.payload[0].DateRan;
e = msg.payload[0].Efficiency.toFixed(2);

pld = "Shift: '"+ s + "'\n";
pld = pld + "DateRan: '"+ d + "'\n";
pld = pld + "Efficiency: '" + e + "'";
pld = pld;

msg.payload = pld;

return msg;

It returns only one shift:


I am having a difficult time figuring out the loop.

Thanks,
Mike



BDM

unread,
Mar 13, 2018, 3:30:19 PM3/13/18
to Node-RED
If I understand correctly you just need to iterate through the entire array with a for loop.

for (i = 0;  i < msg.payload.length;  i++){

s = msg.payload[i].Shift;
d = msg.payload[i].DateRan;
e = msg.payload[i].Efficiency.toFixed(2);


pld = "Shift: '"+ s + "'\n";
pld = pld + "DateRan: '"+ d + "'\n";
pld = pld + "Efficiency: '" + e + "'";
pld = pld;

msg.payload.push(pld);

}

return msg;

}

something like that. Hope it helps.

Mike Arney

unread,
Mar 13, 2018, 3:42:57 PM3/13/18
to Node-RED
Thank you for the fast reply!
I tried the code but it crashes node red. I had this similar problem before I gave up. 

Thanks,
Mike

Nick O'Leary

unread,
Mar 13, 2018, 4:24:10 PM3/13/18
to Node-RED Mailing List
Hi Mike,

there is a bug in suggested function - it is looping over msg.payload and in each loop it is pushing something on the end of msg.payload - which means the for loop never reaches the end and it all goes bang.

Here's a fixed up version:

var pld = "";
for (i = 0;  i < msg.payload.length;  i++){
    if (i > 0) {
        pld += "\n\n";
    }
    pld += "Shift: '"+ msg.payload[i].Shift + "'\n";
    pld += "DateRan: '"+ msg.payload[i].DateRan + "'\n";
    pld += "Efficiency: '" + msg.payload[i].Efficiency.toFixed(2) + "'";
}

msg.payload = pld;
return msg;


Nick

--
http://nodered.org
 
Join us on Slack to continue the conversation: http://nodered.org/slack
---
You received this message because you are subscribed to the Google Groups "Node-RED" group.
To unsubscribe from this group and stop receiving emails from it, send an email to node-red+unsubscribe@googlegroups.com.
To post to this group, send email to node...@googlegroups.com.
Visit this group at https://groups.google.com/group/node-red.
To view this discussion on the web, visit https://groups.google.com/d/msgid/node-red/ee8bd378-01ef-4b2f-86a8-4488119df4ae%40googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

Mike Arney

unread,
Mar 13, 2018, 5:04:08 PM3/13/18
to node...@googlegroups.com
That worked! Thank you so much!

Mike

You received this message because you are subscribed to a topic in the Google Groups "Node-RED" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/node-red/JWoepmJpf8U/unsubscribe.
To unsubscribe from this group and all its topics, send an email to node-red+unsubscribe@googlegroups.com.

To post to this group, send email to node...@googlegroups.com.
Visit this group at https://groups.google.com/group/node-red.

Mike Arney

unread,
Mar 14, 2018, 7:36:40 AM3/14/18
to Node-RED
One last thing. How do I remove the single quote marks from the message? Here is the email that I am receiving:

Shift: '3'
DateRan: 'Wed Mar 14 2018 07:00:02 GMT-0400 (EDT)'
Efficiency: '5.16'

Shift: '2'
DateRan: 'Tue Mar 13 2018 23:06:55 GMT-0400 (EDT)'
Efficiency: '0.00'

Shift: '1'
DateRan: 'Tue Mar 13 2018 15:00:03 GMT-0400 (EDT)'
Efficiency: '9.61'

Nick O'Leary

unread,
Mar 14, 2018, 8:15:03 AM3/14/18
to Node-RED Mailing List
 How do I remove the single quote marks from the message

The single quotes are in the Function code (as they were there in the version you started with).
You just have to remove them from the Function that is building up the text.


var pld = "";
for (i = 0;  i < msg.payload.length;  i++){
    if (i > 0) {
        pld += "\n\n";
    }
    pld += "Shift: "+ msg.payload[i].Shift + "\n";
    pld += "DateRan: "+ msg.payload[i].DateRan + "\n";
    pld += "Efficiency: " + msg.payload[i].Efficiency.toFixed(2);
}




--
http://nodered.org
 
Join us on Slack to continue the conversation: http://nodered.org/slack
---
You received this message because you are subscribed to the Google Groups "Node-RED" group.
To unsubscribe from this group and stop receiving emails from it, send an email to node-red+unsubscribe@googlegroups.com.
To post to this group, send email to node...@googlegroups.com.
Visit this group at https://groups.google.com/group/node-red.

Mike Arney

unread,
Mar 14, 2018, 9:08:32 AM3/14/18
to node...@googlegroups.com
Thank you!

You received this message because you are subscribed to a topic in the Google Groups "Node-RED" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/node-red/JWoepmJpf8U/unsubscribe.
To unsubscribe from this group and all its topics, send an email to node-red+unsubscribe@googlegroups.com.

To post to this group, send email to node...@googlegroups.com.
Visit this group at https://groups.google.com/group/node-red.
Reply all
Reply to author
Forward
0 new messages