# How to pass the property name as a variable?

**URL:** <https://discourse.nodered.org/t/how-to-pass-the-property-name-as-a-variable/45336>\
**Category:** General\
**Created:** [7 May 2021 11:37 UTC](https://discourse.nodered.org/t/how-to-pass-the-property-name-as-a-variable/45336 "2021-05-07T11:37:48Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![akd02](https://avatars.discourse-cdn.com/v4/letter/a/5e9695/32.png) [@akd02](https://discourse.nodered.org/u/akd02)\
**Post date:** [7 May 2021 11:37 UTC](https://discourse.nodered.org/t/how-to-pass-the-property-name-as-a-variable/45336/1 "2021-05-07T11:37:48Z")

</div>

Hello Community,

i m trying to create a **subflow** where the "Environment variables" of the subflow is a property name.

so inside my subflow i could have a function that can then access the value in the property such as:  
msg.payload.{some\_variable\_entered\_from\_the\_sub\_flow\_variables\_box}

is there a way to do that?

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [7 May 2021 12:01 UTC](https://discourse.nodered.org/t/how-to-pass-the-property-name-as-a-variable/45336/2 "2021-05-07T12:01:11Z")

</div>

> [@akd02](#):
>
> so inside my subflow i could have a function that can then access the value in the property such as:  
> msg.payload.{some\_variable\_entered\_from\_the\_sub\_flow\_variables\_box}
> 
> is there a way to do that?

yes.

You can use [square bracket notation](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Property_accessors#bracket_notation)

example...

```javascript
var prop = "payload";
var value = msg[prop];

```

or the node-red helper function `RED.util.getObjectProperty`...

```javascript
var prop = "payload";
var value = RED.util.getObjectProperty(msg,prop);

```

The `getObjectProperty` helper function will also get nested properties e.g...

```javascript
var prop = "payload.data.value";
var value = RED.util.getObjectProperty(msg,prop);

```

As for getting the "Environment variables" - assume you know how to do that already?

---

<div class="post-metadata">

**Author:** ![akd02](https://avatars.discourse-cdn.com/v4/letter/a/5e9695/32.png) [@akd02](https://discourse.nodered.org/u/akd02)\
**Post date:** [10 May 2021 11:14 UTC](https://discourse.nodered.org/t/how-to-pass-the-property-name-as-a-variable/45336/3 "2021-05-10T11:14:24Z")

</div>

thank you for the prompt reply Steve,

I assume getting the Environment Variables is done by using: ${enviroment\_variable}

i tried that in a Function like this: but did not work ☹

var prop = ${enviroment\_variable};  
var value = msg[prop];

---

<div class="post-metadata">

**Author:** ![knolleary](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/knolleary/32/3_2.png) [@knolleary](https://discourse.nodered.org/u/knolleary)\
**Post date:** [10 May 2021 11:17 UTC](https://discourse.nodered.org/t/how-to-pass-the-property-name-as-a-variable/45336/4 "2021-05-10T11:17:03Z")

</div>

Hi @akd02

The docs for using environment variables in your flows are here: [Using environment variables : Node-RED](https://nodered.org/docs/user-guide/environment-variables)

---

<div class="post-metadata">

**Author:** ![akd02](https://avatars.discourse-cdn.com/v4/letter/a/5e9695/32.png) [@akd02](https://discourse.nodered.org/u/akd02)\
**Post date:** [10 May 2021 11:52 UTC](https://discourse.nodered.org/t/how-to-pass-the-property-name-as-a-variable/45336/5 "2021-05-10T11:52:18Z")

</div>

Thanks guys! truly appreciate it

i got it now... below is my working example:

var prop = env.get("Property");

//var value = msg[prop]; // did not work ☹  
var value = RED.util.getObjectProperty(msg,prop); //worked ok!!! 🙂

msg.payload = value; // returns the value inside the Property that was set in the Subflow's template

---

<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:** [9 July 2021 11:52 UTC](https://discourse.nodered.org/t/how-to-pass-the-property-name-as-a-variable/45336/6 "2021-07-09T11:52:55Z")

</div>

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