# Bug updating global arrays

**URL:** <https://discourse.nodered.org/t/bug-updating-global-arrays/2403>\
**Category:** General\
**Created:** [19 August 2018 16:08 UTC](https://discourse.nodered.org/t/bug-updating-global-arrays/2403 "2018-08-19T16:08:54Z")\
**Posts on this page:** 1\
**Showing post:** 3

<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:** [19 August 2018 17:00 UTC](https://discourse.nodered.org/t/bug-updating-global-arrays/2403/3 "2018-08-19T17:00:20Z")

</div>

> [@blechdom](#):
>
> It only fails after I have copied a current\_array to the last\_array. Almost always immediately after I have done so. It's as if, updating the current\_array also updates last\_array,

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.

```auto
var current_array = flow.get("current_array");
flow.set("last_array", current_array);
msg.payload = "set last array to current array";
return msg;

```

Try to use this statement to copy current\_array to last\_array:

```auto
last_array = [...current_array];

```

Only then you save last\_array to the flow context

```auto
flow.set("last_array", last_array);

```

edit:

Therefore this is how your last function will looks like:

```auto
var current_array = flow.get("current_array");
last_array = [...current_array];
flow.set("last_array", last_array);
msg.payload = "set last array to current array";
return msg;

```

---

_[View the full topic](https://discourse.nodered.org/t/bug-updating-global-arrays/2403)._
