Message property set syntax


#1

Hi everyone,
I am pretty new to Node Red and JavaScript in general. I have a question about setting a property of a message. I have the following code snippet:

var uisource = flow.get("source");
output.push([datetime, msg.payload[i][j].uisource]);

I can't figure out the syntax to put the value of the "uisource" string variable to msg.payload[i][j].***** So for example, if I var uisource = "temperature", my second line looks like:
output.push([datetime, msg.payload[i][j].temperature]);

If uisource = "light", then:
output.push([datetime, msg.payload[i][j].light]);

I need the variable uisource to change dinamically and also the identifier of the property I want to access.

Sorry if it is a very amateur question.
Thank you in advance for any help.
Örs


#2

Hi @darabontors

No problem - everyone has to learn this the first time :slight_smile:

You need to use what is called 'bracket notation'.

In fact, your code already does it with the way it uses i and j to reference parts of the message payload... you just need to extend that for uisource as well.

So if uisource is set to the name of the property you want to use, you would do:

msg.payload[i][j][uisource]

#3

Hi Nick,
Wow. That worked like a charm.
Thank you so much!
Have a great day!
Örs