# Global.get changes structure of the context

**URL:** <https://discourse.nodered.org/t/global-get-changes-structure-of-the-context/93092>\
**Category:** General\
**Created:** [12 November 2024 10:36 UTC](https://discourse.nodered.org/t/global-get-changes-structure-of-the-context/93092 "2024-11-12T10:36:50Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Vilnis\_R](https://avatars.discourse-cdn.com/v4/letter/v/a183cd/32.png) [@Vilnis\_R](https://discourse.nodered.org/u/Vilnis_R)\
**Post date:** [12 November 2024 10:36 UTC](https://discourse.nodered.org/t/global-get-changes-structure-of-the-context/93092/1 "2024-11-12T10:36:50Z")

</div>

Hi, I have issue where global.get seems to 'regroup' elements of the object.  
Here is context value copied out from context panel:

```auto
{"group":{"hide":["Tablet_Heat","Tablet_Weather","Tablet_Collector","Tablet_EV","Tablet_Electricity","Tablet_ElectricityP","Tablet_House","Tablet_Pump","Tablet_Carport","Tablet_Workshop","Tablet_Sauna","Tablet_Misc"],"show":["Tablet_Menu"]}}

```

Here is the object that I get after:  
var myArr = global.get("Menu");  
msg.payload = myArr;

```auto
{"group":{"hide":[],"show":["Tablet_Menu","Tablet_Heat","Tablet_Heat","Tablet_Weather","Tablet_Collector","Tablet_Heat","Tablet_House","Tablet_Heat","Tablet_Weather","Tablet_Collector","Tablet_House","Tablet_Carport","Tablet_Carport","Tablet_Carport"]}}

```

My code works fine on JS simulator with the original context. What I am missing here? Thanks. 🙂

```auto
var Show = 'Tablet_Carport';
var myArr = global.get("Menu");

myArr.group.show.push(Show);
var index = myArr.group.hide.indexOf(Show);
myArr.group.hide.splice(index, 1);

msg.payload = myArr;
return msg

```

---

<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:** [12 November 2024 10:49 UTC](https://discourse.nodered.org/t/global-get-changes-structure-of-the-context/93092/2 "2024-11-12T10:49:19Z")

</div>

Although from what you have written here, i cannot see it but this is almost always due to objects/arrays being stored by reference.

e.g. If you store an object/array in global of flow context, then you edit the object/array later on, you will affect the stored value (because you only stored a pointer/reference to the original)

If you did not want that to happen, be sure to store a clone. You can use the `deep copy` option on a change node OR in a function you can use `RED.util.cloneMessage(xxx)`

e.g.

```auto
const menuClone = RED.util.cloneMessage(myArr)
global.set('Menu', menuClone)

```

---

<div class="post-metadata">

**Author:** ![Vilnis\_R](https://avatars.discourse-cdn.com/v4/letter/v/a183cd/32.png) [@Vilnis\_R](https://discourse.nodered.org/u/Vilnis_R)\
**Post date:** [12 November 2024 11:08 UTC](https://discourse.nodered.org/t/global-get-changes-structure-of-the-context/93092/3 "2024-11-12T11:08:52Z")

</div>

> [@Steve-Mcl](#):
>
> `const menuClone = RED.util.cloneMessage(myArr)`

Thanks a lot! Yes, I did it as You suggested and now it seems to work as intended. I had no idea that if I am not using global.set I am still modifying the context. Does it apply to flow and node contexts too? I am using node and flow contexts a lot but did not noticed that behavior before.

Here in my code I do not need to global.set as I am just grabbing the object, modify and pass it over to ui control node. My code now is:

```auto
var Show = 'Tablet_Carport';
const myArr = RED.util.cloneMessage(global.get('Menu'));

myArr.group.show.push(Show);
var index = myArr.group.hide.indexOf(Show);
myArr.group.hide.splice(index, 1);

msg.payload = myArr;
return msg

```

---

<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:** [12 November 2024 11:09 UTC](https://discourse.nodered.org/t/global-get-changes-structure-of-the-context/93092/4 "2024-11-12T11:09:54Z")

</div>

> [@Vilnis\_R](#):
>
> Does it apply to flow and node contexts too?

Yes, if it is an object (Arrays are objects)

---

<div class="post-metadata">

**Author:** ![Vilnis\_R](https://avatars.discourse-cdn.com/v4/letter/v/a183cd/32.png) [@Vilnis\_R](https://discourse.nodered.org/u/Vilnis_R)\
**Post date:** [12 November 2024 11:12 UTC](https://discourse.nodered.org/t/global-get-changes-structure-of-the-context/93092/5 "2024-11-12T11:12:00Z")

</div>

> [@Steve-Mcl](#):
>
> RED.util.cloneMessage

For the node & flow contexts the same RED.util.cloneMessage applies?

---

<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:** [12 November 2024 11:15 UTC](https://discourse.nodered.org/t/global-get-changes-structure-of-the-context/93092/6 "2024-11-12T11:15:52Z")

</div>

Yes. Context is context (regardless of scope). If you store an object, it is a reference & can be modified without calling `.set`

---

<div class="post-metadata">

**Author:** ![Vilnis\_R](https://avatars.discourse-cdn.com/v4/letter/v/a183cd/32.png) [@Vilnis\_R](https://discourse.nodered.org/u/Vilnis_R)\
**Post date:** [12 November 2024 11:24 UTC](https://discourse.nodered.org/t/global-get-changes-structure-of-the-context/93092/7 "2024-11-12T11:24:46Z")

</div>

Great, today I learned something new that will shorten my codes. 🙂 Thanks again!

---

<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:** [12 November 2024 11:33 UTC](https://discourse.nodered.org/t/global-get-changes-structure-of-the-context/93092/8 "2024-11-12T11:33:55Z")

</div>

> [@Vilnis\_R](#):
>
> For the node & flow contexts the same RED.util.cloneMessage applies?

That command is not directly related to context. It will clone any object.

---

<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:** [26 November 2024 11:34 UTC](https://discourse.nodered.org/t/global-get-changes-structure-of-the-context/93092/9 "2024-11-26T11:34:25Z")

</div>

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