# Changing Data without changing Variable

**URL:** <https://discourse.nodered.org/t/changing-data-without-changing-variable/31183>\
**Category:** General\
**Created:** [5 August 2020 15:00 UTC](https://discourse.nodered.org/t/changing-data-without-changing-variable/31183 "2020-08-05T15:00:47Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![thomas](https://avatars.discourse-cdn.com/v4/letter/t/e36b37/32.png) [@thomas](https://discourse.nodered.org/u/thomas)\
**Post date:** [5 August 2020 15:00 UTC](https://discourse.nodered.org/t/changing-data-without-changing-variable/31183/1 "2020-08-05T15:00:47Z")

</div>

Hello,

I have got a problem with global variables.  
The global variable changes its value even when I don't apply global.set(). How does it come and how can I prevent it. It looks like the variable in the flow is still connected to the global one.

I built a little flow for the demonstration but I cant upload a file.

`[{"id":"dc859282.3c8a7","type":"inject","z":"dd2b1b78.857558","name":"","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":600,"y":160,"wires":[["f842a5f3.e01728"]]},{"id":"f842a5f3.e01728","type":"function","z":"dd2b1b78.857558","name":"set global Obj","func":"var global_obj = {\n \"array_1\" : [1,2,3],\n \"array_2\" : [4,5,6],\n \"array_3\" : [[1,2,3] , [7,null,9], [4,5,6]],\n};\n\nglobal.set(\"global_obj\", global_obj);\nreturn msg;","outputs":1,"noerr":0,"x":810,"y":160,"wires":[["11d65e6b.112022"]]},{"id":"11d65e6b.112022","type":"debug","z":"dd2b1b78.857558","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":1010,"y":160,"wires":[]},{"id":"35096b3b.a84954","type":"inject","z":"dd2b1b78.857558","name":"","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":600,"y":240,"wires":[["7c2be196.5bb1"]]},{"id":"7c2be196.5bb1","type":"function","z":"dd2b1b78.857558","name":"Replace null","func":"var data_ist = global.get(\"global_obj\").array_3[1];\n\nfor (let i in data_ist){\n if (data_ist[i] ===null){\n if (i >= 1){\n if (data_ist[(i-1)] !== null){\n var lower_value = data_ist[i-1];\n data_ist[i] = lower_value ;\n }\n \n }\n }\n}\nmsg.payload = data_ist;\nreturn msg;","outputs":1,"noerr":0,"x":790,"y":240,"wires":[["b71d41f5.a5bf6"]]},{"id":"b71d41f5.a5bf6","type":"debug","z":"dd2b1b78.857558","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":1010,"y":240,"wires":[]}]`

Thanks for your help.

Thomas

---

<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:** [5 August 2020 15:02 UTC](https://discourse.nodered.org/t/changing-data-without-changing-variable/31183/2 "2020-08-05T15:02:52Z")

</div>

This is because of the way JavaScript passes objects around by reference...

This previous thread explains the details and how to handle it - [Global memory updates even if I don't "save" back to it](https://discourse.nodered.org/t/global-memory-updates-even-if-i-dont-save-back-to-it/16326)

---

<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:** [19 August 2020 15:03 UTC](https://discourse.nodered.org/t/changing-data-without-changing-variable/31183/3 "2020-08-19T15:03:02Z")

</div>

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