# Variable as message property

**URL:** <https://discourse.nodered.org/t/variable-as-message-property/34162>\
**Category:** General\
**Created:** [13 October 2020 07:45 UTC](https://discourse.nodered.org/t/variable-as-message-property/34162 "2020-10-13T07:45:40Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![bigfeetneedbigboots](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bigfeetneedbigboots/32/33216_2.png) [@bigfeetneedbigboots](https://discourse.nodered.org/u/bigfeetneedbigboots)\
**Post date:** [13 October 2020 07:45 UTC](https://discourse.nodered.org/t/variable-as-message-property/34162/1 "2020-10-13T07:45:40Z")

</div>

Hello folks,

I am trying to compare a number of message properties in a function node and would like to do this with variables rather than repeating each object path. The trouble I am having is that I can't work out how to build the variables into the object path.

What I have so far is:

```auto
//variables
var current = "v_hallightgrp";
var target = "c_lightnight";

//compare brightness
if (msg.payload.+current+.brightness !== msg.payload.+target+.brightness) {

```

This is throwing an error. I know putting it in quotes isn't the answer as that converts the object paths to strings. Thanks in advance for your help!

Edit:  
Strings defined correctly for variables (thanks Johannes)

---

<div class="post-metadata">

**Author:** ![JGKK](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/jgkk/32/18515_2.png) [@JGKK](https://discourse.nodered.org/u/JGKK)\
**Post date:** [13 October 2020 07:56 UTC](https://discourse.nodered.org/t/variable-as-message-property/34162/2 "2020-10-13T07:56:14Z")

</div>

You can use a variable as an object key by putting it in square brackets. For example

```auto
const key = "test";
msg.payload = msg.payload[key];
return msg;

```

Would return `msg.payload.test` as the `msg.payload`.

Johannes

Edit:

Also

> [@bigfeetneedbigboots](#):
>
> ```auto
> var current = v_hallightgrp;
> var target = c_lightnight;
> 
> ```

Where did you define `v_hallightgrp` and `c_lightnight` or did you mean them to be strings? Because the way you wrote it above without `"` before and after is trying to reference a variable of the name `v_hallightgrp` that in itself would have to be defined somewhere.

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [13 October 2020 09:09 UTC](https://discourse.nodered.org/t/variable-as-message-property/34162/3 "2020-10-13T09:09:37Z")

</div>

> [@bigfeetneedbigboots](#):
>
> `if (msg.payload.+current+.brightness !== msg.payload.+target+.brightness) {`

That should be  
`if (msg.payload[current].brightness !== msg.payload[target].brightness) {`  
The dot syntax is a shorthand for the square bracket notation, so the above line is actually a shorthand way of writing  
`if (msg["payload"][current]["brightness"] !== ....`

---

<div class="post-metadata">

**Author:** ![bigfeetneedbigboots](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bigfeetneedbigboots/32/33216_2.png) [@bigfeetneedbigboots](https://discourse.nodered.org/u/bigfeetneedbigboots)\
**Post date:** [13 October 2020 09:49 UTC](https://discourse.nodered.org/t/variable-as-message-property/34162/4 "2020-10-13T09:49:30Z")

</div>

Thank you Johannes and Colin. I've tested and got it working now. Much obliged.

Joannes, well spotted, I meant them to be strings. I've edited the OP so as to help others in the future.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)\
**Post date:** [27 October 2020 09:49 UTC](https://discourse.nodered.org/t/variable-as-message-property/34162/5 "2020-10-27T09:49:33Z")

</div>

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