Arrays & Objects in Node-RED

17,903 views
Skip to first unread message

Ram

unread,
Apr 1, 2017, 10:14:04 AM4/1/17
to Node-RED
Hi,
I've an output from one of my flow as shown below. I'm trying to get the objects Level, UnitNumber, Schools & Comfort as a single CSV file. How can I convert this array into a merged single output?

msg.payload : array[1]
array[1]
0object
Levelarray[1]
0"Level 6"
UnitNumberarray[1]
0"10"
Schoolsarray[1]
0"Office A"
Comfortarray[1]
0"Freezing"

Mark Setrem

unread,
Apr 1, 2017, 11:09:04 AM4/1/17
to Node-RED

As i suggested on the 23rd to one of your other "how do I reformat this" questions.  Some time reading a Javascript Arrays and Objects tutorial would be time well spend.

My personal favourite is https://www.w3schools.com/js/js_arrays.asp   

If you get stuck, post what you've managed to do here and I'm sure someone will help you correct it.




Message has been deleted

Dj Jastrzębowski

unread,
Jan 1, 2018, 7:27:15 AM1/1/18
to Node-RED
Could anyone help me and tell me how to put a variable instead of expressing the "sensor" in the code to accept the variable var

var sensor = msg.topic


var data = msg.payload;
msg.measurement = global.get ("temp");
msg.payload = [{
sensor: parseFloat(data),
}];
return msg;

 

Dave C-J

unread,
Jan 1, 2018, 8:00:59 AM1/1/18
to Node-RED
If it's always going to be an array you want then something like this
...
msg.payload = [];  // create an empty array
msg.payload[0][msg.topic] = parseFloat(data);
...

Dave C-J

unread,
Jan 1, 2018, 8:06:16 AM1/1/18
to node...@googlegroups.com
ooops

missed a line

...
msg.payload = []; // create an empty array
msg.payload[0] = {};  // create an empty object as first item
msg.payload[0][session] = parseFloat(data);
...
Message has been deleted

Colin Law

unread,
Jan 1, 2018, 11:53:31 AM1/1/18
to node...@googlegroups.com
What are you seeing in the payload for the first case?  It is no good saying it is not working unless you tell us what you are seeing and what you want it to be.

Colin

On 1 January 2018 at 16:47, Dj Jastrzębowski <dariuszjas...@gmail.com> wrote:
Thanks Dave,

I dont know not working :-(

var data = msg.payload;
var data1 = global.get ("variable1");
msg.measurement = global.get ("variable2");
msg.payload = []; // create an empty array
msg.payload[0] = {};  // create an empty object as first item
msg.payload[0][global.get ("casevariable")] = parseFloat(data);
return msg;


and try 

var data = msg.payload;
var data1 = global.get ("casevariable");
msg.measurement = global.get ("prodvariable1");
msg.payload = []; // create an empty array
msg.payload[0] = {};  // create an empty object as first item
msg.payload[0][(data1)] = parseFloat(data);
return msg;


Array return 


--
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/c5453089-322f-47f9-8c26-472cc1dc3938%40googlegroups.com.

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

Colin Law

unread,
Jan 1, 2018, 11:55:23 AM1/1/18
to node...@googlegroups.com
The second one doesn't work because you need a string, so msg.payload[0]["data1"].

Colin

On 1 January 2018 at 16:52, Colin Law <cla...@gmail.com> wrote:
What are you seeing in the payload for the first case?  It is no good saying it is not working unless you tell us what you are seeing and what you want it to be.

Colin

Colin Law

unread,
Jan 1, 2018, 12:07:22 PM1/1/18
to node...@googlegroups.com
On 1 January 2018 at 16:54, Colin Law <cla...@gmail.com> wrote:
The second one doesn't work because you need a string, so msg.payload[0]["data1"].

Sorry, that is wrong for the second one, it should be msg.payload[0][data1] as data1 is already a string (presumably).

Colin

 

Colin

On 1 January 2018 at 16:52, Colin Law <cla...@gmail.com> wrote:
What are you seeing in the payload for the first case?  It is no good saying it is not working unless you tell us what you are seeing and what you want it to be.

Colin

Dj Jastrzębowski

unread,
Jan 1, 2018, 12:46:23 PM1/1/18
to Node-RED
I have global variables declared as text (to all Node-red) and payload input in to node function as value

variable1 - text with space (debug its is ok) the variable is well declared
variable2 - text need to send influxdatabase
msg.payload - numeric value 

var data = msg.payload;
var data1 = global.get ("variable1");
msg.measurement = global.get ("variable2");
msg.payload = []; // create an empty array
msg.payload[0] = {};  // create an empty object as first item
msg.payload[0][(data1)] = parseFloat(data);
return msg;


Output debug say 


 

Colin Law

unread,
Jan 1, 2018, 1:05:12 PM1/1/18
to node...@googlegroups.com
You should not have the parentheses round data1, though I would not expect it to matter, but it should be, as I said earlier
msg.payload[0][data1] = parseFloat(data);
Put a line in above that with
node.warn("data1 is " + data1 + " type is " + typeof data1);
It will print that in the debug window so you can see what it says

Colin

--
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.

Dj Jastrzębowski

unread,
Jan 1, 2018, 1:17:28 PM1/1/18
to Node-RED
Thank you for professional help

 

Colin Law

unread,
Jan 1, 2018, 4:41:56 PM1/1/18
to node...@googlegroups.com
Glad to be of help, but it is certainly not professional. I retired 11 years ago.

Please tell us what the issue was so that others finding this (and us) may learn something.

Colin

On 1 January 2018 at 18:17, Dj Jastrzębowski <dariuszjas...@gmail.com> wrote:
Thank you for professional help

 

--
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.

Dj Jastrzębowski

unread,
Jan 4, 2018, 2:05:42 PM1/4/18
to Node-RED
I have to admit there was something wrong with my variable declaration :-(

but it's ok with

Colin Law

unread,
Jan 4, 2018, 3:43:08 PM1/4/18
to node...@googlegroups.com
Take the parentheses out round data1, in the future they will just confuse you, or someone else. You will wonder what subtle reason there was for including them.

Colin

--
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.
Reply all
Reply to author
Forward
0 new messages