# Is there a way to set context properties dynamically?

**URL:** <https://discourse.nodered.org/t/is-there-a-way-to-set-context-properties-dynamically/27760>\
**Category:** General\
**Created:** [4 June 2020 10:26 UTC](https://discourse.nodered.org/t/is-there-a-way-to-set-context-properties-dynamically/27760 "2020-06-04T10:26:23Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![AAAAnT](https://avatars.discourse-cdn.com/v4/letter/a/db5fbb/32.png) [@AAAAnT](https://discourse.nodered.org/u/AAAAnT)\
**Post date:** [4 June 2020 10:26 UTC](https://discourse.nodered.org/t/is-there-a-way-to-set-context-properties-dynamically/27760/1 "2020-06-04T10:26:24Z")

</div>

Hello

I am working with a flow where I have multiple identification key saved in a JSON flow variable, as in the following example:

```auto
{
  "info": {
    "id_1": {
      "previous": "value_1",
      "key_2": "value_2",
      ...
    },
    "id_2": {
    ...
    },
  ...
}

```

The point is that each identification key stores that sort of "previous" value, which I want to save into the flow variable, because a lot of the functionality of the flow is based on comparing new/current value to the previous value. The new/current values are retrieved using HTTP request so that the identification key is received as the response, along with the new/current value. This is why I would like to be able to set the flow variables dynamically in a loop, like:

```auto
for (var i = 0; i < responses.length; ++i) {
  var current_id = responses[i].id;
  var new_value = responses[i].new_value;
  flow.set("info[current_id]['previous']", new_value); 
}

```

It does not need to be done exactly like this, but I wanted to know if it is possible to set flow variables dynamically. There are no examples in the web that I found.

Thanks in advance!

---

<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:** [4 June 2020 10:42 UTC](https://discourse.nodered.org/t/is-there-a-way-to-set-context-properties-dynamically/27760/2 "2020-06-04T10:42:11Z")

</div>

> [@AAAAnT](#):
>
> `flow.set("info[current_id]['previous']", new_value);`

The first parameter of flow.set is a string, it does not need to be a constant, it can be the value of a variable, so assuming that `info[current_id]['previous']` contains a string then you want

```auto
flow.set(info[current_id]['previous'], new_value)

```

Note, no quotes around the variable, otherwise it will interpret the whole thing as the name of the context variable rather than using the contents of the variable.

---

<div class="post-metadata">

**Author:** ![AAAAnT](https://avatars.discourse-cdn.com/v4/letter/a/db5fbb/32.png) [@AAAAnT](https://discourse.nodered.org/u/AAAAnT)\
**Post date:** [4 June 2020 10:51 UTC](https://discourse.nodered.org/t/is-there-a-way-to-set-context-properties-dynamically/27760/3 "2020-06-04T10:51:29Z")

</div>

> ```auto
> flow.set(info[current_id]['previous'], new_value)
> 
> ```

Is there something I am missing? If I try this I get:

```auto
"ReferenceError: info is not defined"

```

Should Node be able to find the correct context property without quotation marks around the first item?

---

<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:** [4 June 2020 10:54 UTC](https://discourse.nodered.org/t/is-there-a-way-to-set-context-properties-dynamically/27760/4 "2020-06-04T10:54:34Z")

</div>

Try with:

```auto
flow.set( "info['" + current_id + "']['previous']", new_value)

```

---

<div class="post-metadata">

**Author:** ![AAAAnT](https://avatars.discourse-cdn.com/v4/letter/a/db5fbb/32.png) [@AAAAnT](https://discourse.nodered.org/u/AAAAnT)\
**Post date:** [4 June 2020 10:56 UTC](https://discourse.nodered.org/t/is-there-a-way-to-set-context-properties-dynamically/27760/5 "2020-06-04T10:56:51Z")

</div>

> ```auto
> flow.set( "info['" + current_id + "']['previous']", new_value)
> 
> ```

This worked, thank you Nick!

---

<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:** [4 June 2020 11:16 UTC](https://discourse.nodered.org/t/is-there-a-way-to-set-context-properties-dynamically/27760/6 "2020-06-04T11:16:01Z")

</div>

Oh yes, I misinterpreted the requirement.

---

<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:** [18 June 2020 11:16 UTC](https://discourse.nodered.org/t/is-there-a-way-to-set-context-properties-dynamically/27760/7 "2020-06-18T11:16:08Z")

</div>

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