# Directly modify context as reference

**URL:** <https://discourse.nodered.org/t/directly-modify-context-as-reference/87802>\
**Category:** General\
**Tags:** function-node\
**Created:** [8 May 2024 09:14 UTC](https://discourse.nodered.org/t/directly-modify-context-as-reference/87802 "2024-05-08T09:14:18Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Lupin\_III](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/lupin_iii/32/67650_2.png) [@Lupin\_III](https://discourse.nodered.org/u/Lupin_III)\
**Post date:** [8 May 2024 09:14 UTC](https://discourse.nodered.org/t/directly-modify-context-as-reference/87802/1 "2024-05-08T09:14:19Z")

</div>

Hi, working with context (context,flow or global) we directly access and modify them unless we don't use RED.util.cloneMessage. So if for instance we have

`flow.set("mytest",{ "A":[1,2,3], "B":{"value1":"B1","value2":"B2"},"C":[10,20,30]})`  
doing

```auto
var mynewtest = flow.get("mytest")
mynewtest.C = ["a","b","c"]

```

it results in  
flow.get("mytest") = { "A":[1,2,3], "B":{"value1":"B1","value2":"B2"},"C":["a","b","c"]}  
so mynewtest modify context mytest as expected

But if we do

```auto
var mynewtest = flow.get("mytest.C")
mynewtest = ["a","b","c"]

```

it results in  
flow.get("mytest") = { "A":[1,2,3], "B":{"value1":"B1","value2":"B2"},"C":[10,20,30]})  
instead of  
flow.get("mytest") = { "A":[1,2,3], "B":{"value1":"B1","value2":"B2"},"C":["a","b","c"]}  
In this case mynewtest doesn't modify context mytest. Is that correct?

---

<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:** [8 May 2024 13:34 UTC](https://discourse.nodered.org/t/directly-modify-context-as-reference/87802/2 "2024-05-08T13:34:01Z")

</div>

> [@Lupin\_III](#):
>
> ```auto
> var mynewtest = flow.get("mytest.C")
> mynewtest = ["a","b","c"]
> 
> ```

> [@Lupin\_III](#):
>
> In this case mynewtest doesn't modify context mytest. Is that correct?

Correct. The first line sets mynewtext to point to the object in the context memory.  
The second line does not change a property of mynewtext (which would change the value in context) but simply sets the value of mynewtest to point to a newly created object containing the array.

By the way, it is best not to use `var` nowadays. One should use `const` or `let`. Your favourite search engine will tell you why if you want to know.

---

<div class="post-metadata">

**Author:** ![Lupin\_III](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/lupin_iii/32/67650_2.png) [@Lupin\_III](https://discourse.nodered.org/u/Lupin_III)\
**Post date:** [8 May 2024 13:37 UTC](https://discourse.nodered.org/t/directly-modify-context-as-reference/87802/3 "2024-05-08T13:37:19Z")

</div>

Thanks so much @Colin

---

<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:** [6 August 2024 13:37 UTC](https://discourse.nodered.org/t/directly-modify-context-as-reference/87802/4 "2024-08-06T13:37:20Z")

</div>

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