# Copying array and empty the original

**URL:** <https://discourse.nodered.org/t/copying-array-and-empty-the-original/42705>\
**Category:** General\
**Created:** [16 March 2021 09:40 UTC](https://discourse.nodered.org/t/copying-array-and-empty-the-original/42705 "2021-03-16T09:40:36Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![S.Luent](https://avatars.discourse-cdn.com/v4/letter/s/db5fbb/32.png) [@S.Luent](https://discourse.nodered.org/u/S.Luent)\
**Post date:** [16 March 2021 09:40 UTC](https://discourse.nodered.org/t/copying-array-and-empty-the-original/42705/1 "2021-03-16T09:40:36Z")

</div>

Hey,  
i'm new to Node-Red. I'm sorry if there is already an answer but i couldn't find any in this forum.

The function:  
I want to store data in an array (Speicher1) and after a specific time, i want to copy that array (Speicher1) into another array (Speicher2) and delete/empty the original array (speicher1). Both arrays are a flow variable.

My solution:

```auto
var tempSpeicher1 = flow.get('Speicher1');

flow.set('Speicher2', tempSpeicher1);
tempSpeicher1.length= 0;
flow.set('Speicher1', tempSpeicher1);

msg.payload = 'Speicher2: ' +flow.get('Speicher2') + 'Speicher1: '+ flow.get('Speicher1');

return msg;

```

The problem:  
After this function both of the arrays are empty.

If i delete `tempSpeicher1.length = 0` it will copy the array "Speicher1" to "Speicher2".

---

<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:** [16 March 2021 09:52 UTC](https://discourse.nodered.org/t/copying-array-and-empty-the-original/42705/2 "2021-03-16T09:52:21Z")

</div>

What you are doing is setting a reference of `Speicher1` to `Speicher2` so when you change `Speicher1` you change `Speicher2`.

you can get around this (depending on the contents of the array being immutable or not).

An quick (but shallow) method would be to use the spread operator like this...

```auto
var Speicher2 = [...Speicher1]; 

```

---

<div class="post-metadata">

**Author:** ![S.Luent](https://avatars.discourse-cdn.com/v4/letter/s/db5fbb/32.png) [@S.Luent](https://discourse.nodered.org/u/S.Luent)\
**Post date:** [16 March 2021 09:57 UTC](https://discourse.nodered.org/t/copying-array-and-empty-the-original/42705/3 "2021-03-16T09:57:35Z")

</div>

Thanks for your quick answer. It works for me now with this code:

```auto
var tempSpeicher1 = flow.get('Speicher1');
var tempSpeicher2 = [...tempSpeicher1];

flow.set('Speicher2', tempSpeicher2);
tempSpeicher1.length= 0;
flow.set('Speicher1', tempSpeicher1);

msg.payload = 'Speicher2: ' +flow.get('Speicher2') + 'Speicher1: '+ flow.get('Speicher1');

return msg;

```

Is there a way to use the spread operator directly with flow variables?

---

<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:** [16 March 2021 10:00 UTC](https://discourse.nodered.org/t/copying-array-and-empty-the-original/42705/4 "2021-03-16T10:00:41Z")

</div>

> [@S.Luent](#):
>
> Is there a way to use the spread operator directly with flow variables?

Its just JavaScript...

```auto
flow.set('Speicher2', [...(flow.get('Speicher1') || []) ] );

```

the `|| []` part ensures it doesnt error if `flow.get('Speicher1')` is empty. (`|| []` returns an empty array if `flow.get('Speicher1')` is empty/null)

---

<div class="post-metadata">

**Author:** ![S.Luent](https://avatars.discourse-cdn.com/v4/letter/s/db5fbb/32.png) [@S.Luent](https://discourse.nodered.org/u/S.Luent)\
**Post date:** [16 March 2021 10:11 UTC](https://discourse.nodered.org/t/copying-array-and-empty-the-original/42705/5 "2021-03-16T10:11:37Z")

</div>

Thank you.

---

<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:** [30 March 2021 10:12 UTC](https://discourse.nodered.org/t/copying-array-and-empty-the-original/42705/6 "2021-03-30T10:12:22Z")

</div>

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