# How do I make a copy of a global object without changing it?

**URL:** <https://discourse.nodered.org/t/how-do-i-make-a-copy-of-a-global-object-without-changing-it/10087>\
**Category:** General\
**Created:** [13 April 2019 00:25 UTC](https://discourse.nodered.org/t/how-do-i-make-a-copy-of-a-global-object-without-changing-it/10087 "2019-04-13T00:25:18Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![zenofmud](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/zenofmud/32/316_2.png) [@zenofmud](https://discourse.nodered.org/u/zenofmud)\
**Post date:** [13 April 2019 00:25 UTC](https://discourse.nodered.org/t/how-do-i-make-a-copy-of-a-global-object-without-changing-it/10087/1 "2019-04-13T00:25:19Z")

</div>

macOS High Sierra 10.13.6  
Node-RED version: v0.20.5  
Node.js version: v10.15.3  
Darwin 17.7.0 x64 LE

I seem to have run into an issue using a global where I'm not using a copy of it, but a pointer. I do this:

```auto
var arr1 = global.get('foo')||[];
var rows=arr1;

```

and when I make changes `rows` the global is changed. Attached is a very simple set of flows that shows the problem. My question is how do I make a copy of the object so I don't change the original?

[globalpointer.json](https://discourse.nodered.org/uploads/short-url/vzRGIxcMoivHQtASnM8bTtRk9Qy.json) (2.6 KB)

![49%20PM](https://cdck-file-uploads-us1.s3.dualstack.us-west-2.amazonaws.com/flex026/uploads/nodered/original/2X/4/4891f7c64d3425921f4e92124a415c318e8f80f6.png)

---

<div class="post-metadata">

**Author:** ![Andrei](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/andrei/32/10446_2.png) [@Andrei](https://discourse.nodered.org/u/Andrei)\
**Post date:** [13 April 2019 01:12 UTC](https://discourse.nodered.org/t/how-do-i-make-a-copy-of-a-global-object-without-changing-it/10087/2 "2019-04-13T01:12:07Z")

</div>

Hi Paul, perhaps a shallow copy of the array ? Please have a look in this post.

> [@Bug updating global arrays](https://discourse.nodered.org/t/bug-updating-global-arrays/2403/3):
>
> HI, indeed, this unwanted behavior will happen when you try to copy an array to another array using the assignment operator (or using the flow.set like you did). In JavaScript assigning current\_array to last\_array will make last\_array to point to the same elements of current\_array, i.e, changing current\_array will affect last\_array and vice\_versa. var current\_array = flow.get("current\_array"); flow.set("last\_array", current\_array); msg.payload = "set last array to current array"; 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:** [13 April 2019 06:47 UTC](https://discourse.nodered.org/t/how-do-i-make-a-copy-of-a-global-object-without-changing-it/10087/3 "2019-04-13T06:47:20Z")

</div>

> [@zenofmud](#):
>
> ```auto
> var arr1 = global.get('foo')||[];
> var rows=arr1;
> 
> ```
> 
> and when I make changes `rows` the global is changed.

Try...

```auto
var arr1 = global.get('foo')||[];
var rows=[...arr1];

```

Note however, if the items in the array are objects and you change a property e.g. `arr1[0].prop = 7` you will have the same issue.

This is normal JavaScript behaviour.

However adding and removing elements to the copy array won't change the original array.

If you are storing objects in the array and need copys of them in your copy array too, have a look on internet for "JavaScript copy object" then loop though or call .map() and make copies of the elements.

---

<div class="post-metadata">

**Author:** ![zenofmud](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/zenofmud/32/316_2.png) [@zenofmud](https://discourse.nodered.org/u/zenofmud)\
**Post date:** [13 April 2019 08:28 UTC](https://discourse.nodered.org/t/how-do-i-make-a-copy-of-a-global-object-without-changing-it/10087/4 "2019-04-13T08:28:04Z")

</div>

@Andrei Thank you for the link pointing to [https://www.youtube.com/watch?v=duyshh9Fs1U](https://www.youtube.com/watch?v=duyshh9Fs1U)

For anyone reading this thread who doesn't understand a shallow vrs deep copy of an object, go watch this video. The author does a great job of explaining it!
