# Issues with variables

**URL:** https://discourse.nodered.org/t/issues-with-variables/71121
**Category:** General
**Tags:** function-node
**Created:** [23 November 2022 15:03 UTC](https://discourse.nodered.org/t/issues-with-variables/71121 "2022-11-23T15:03:07Z")
**Posts on this page:** 12
**Page:** 1

<div class="post-metadata">

### Author: ![DavePF](https://avatars.discourse-cdn.com/v4/letter/d/48db29/32.png) [@DavePF](https://discourse.nodered.org/u/DavePF)
#### Post date: [23 November 2022 15:03 UTC](https://discourse.nodered.org/t/issues-with-variables/71121/1 "2022-11-23T15:03:07Z")

</div>

Hello everyone,

I'm having some problem with basic variables manipulations. Here's a code example that does the issue.

```auto
let CopyOfOriginal = context.get('CopyOfOriginal') || [];

let OriginalObject = 
{
    payload:'Im original'
}

CopyOfOriginal.push(OriginalObject);

context.set('CopyOfOriginal', CopyOfOriginal);

// Modify after the context.set
OriginalObject.payload = 'Im modified';

// Push without context.set
CopyOfOriginal.push(OriginalObject);

return msg;

```

I'm expecting the CopyOfOriginal object to contain 'Im original' since it was copied before the modification was made to the OriginalObject.

And why is the last line able to modify the context object without a context.set ?

Thanks for taking time to have a look at this.

---

<div class="post-metadata">

### Author: ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)
#### Post date: [23 November 2022 15:16 UTC](https://discourse.nodered.org/t/issues-with-variables/71121/2 "2022-11-23T15:16:40Z")

</div>

I think that you have fallen foul of a JavaScript oddity that only applies to context variables _in memory_. If you tried the same thing with some other storage mechanisms, you would get the result you expect.

The reason is that assigning in-memory objects happens _by reference_ - in other words, it is the SAME data, not a copy.

If you want to be certain that you have a copy, there is a `RED` utility method for that - it does a shallow copy. Sorry, I can't remember the method name off the top of my head.

---

<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: [23 November 2022 15:38 UTC](https://discourse.nodered.org/t/issues-with-variables/71121/3 "2022-11-23T15:38:28Z")

</div>

> [@TotallyInformation](#):
>
> If you tried the same thing with some other storage mechanisms, you would get the result you expect.

I am not sure about that, at least not with the standard file storage context. At run time it is still in memory, it is just saved to disc occasionally and restored on restart.

`let CopyOfOriginal = RED.util.cloneMessage(context.get('CopyOfOriginal')) || [];`

---

<div class="post-metadata">

### Author: ![DavePF](https://avatars.discourse-cdn.com/v4/letter/d/48db29/32.png) [@DavePF](https://discourse.nodered.org/u/DavePF)
#### Post date: [23 November 2022 15:47 UTC](https://discourse.nodered.org/t/issues-with-variables/71121/4 "2022-11-23T15:47:20Z")

</div>

Thanks for your answer. I digged a little more and found that I could make a shallow copy using

```auto
CopyOfOriginal.push({ ...OriginalObject });

```

Or a safer deep copy using

```auto
CopyOfOriginal.push(JSON.parse(JSON.stringify(OriginalObject)));

```

---

<div class="post-metadata">

### Author: ![marcus-j-davies](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/marcus-j-davies/32/103435_2.png) [@marcus-j-davies](https://discourse.nodered.org/u/marcus-j-davies)
#### Post date: [23 November 2022 16:00 UTC](https://discourse.nodered.org/t/issues-with-variables/71121/5 "2022-11-23T16:00:12Z")

</div>

Use the already built in helpers 😁

`CopyOfOriginal = RED.util.cloneMessage(CopyOfOriginal)`

`CopyOfOriginal` is no longer a reference type

EDIT  
Uhh I should have read @Colin's message 👼

---

<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: [23 November 2022 16:11 UTC](https://discourse.nodered.org/t/issues-with-variables/71121/6 "2022-11-23T16:11:16Z")

</div>

> [@marcus-j-davies](#):
>
> `CopyOfOriginal` is no longer a reference type

Strictly it is still a reference type, but it is referring to a copy, not the original.

---

<div class="post-metadata">

### Author: ![DavePF](https://avatars.discourse-cdn.com/v4/letter/d/48db29/32.png) [@DavePF](https://discourse.nodered.org/u/DavePF)
#### Post date: [23 November 2022 16:38 UTC](https://discourse.nodered.org/t/issues-with-variables/71121/7 "2022-11-23T16:38:30Z")

</div>

What would be the best practice way of making a deep copy ?

```auto
CopyOfOriginal = JSON.parse(JSON.stringify(OriginalObject));

```

or

```auto
CopyOfOriginal = RED.util.cloneMessage(CopyOfOriginal)

```

---

<div class="post-metadata">

### Author: ![marcus-j-davies](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/marcus-j-davies/32/103435_2.png) [@marcus-j-davies](https://discourse.nodered.org/u/marcus-j-davies)
#### Post date: [23 November 2022 17:00 UTC](https://discourse.nodered.org/t/issues-with-variables/71121/8 "2022-11-23T17:00:06Z")

</div>

`cloneMessage` IMO.

`cloneMessage`, uses the clone package, that walks the object, and guards against circular references.

`JSON.parse(JSON.stringify(OriginalObject))` whilst works, can get into trouble with circular references, may also be not as performant, having to construct a string before it can create another object.

although in everyday use - you might not measure such difference, but there will be.

---

<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: [23 November 2022 17:24 UTC](https://discourse.nodered.org/t/issues-with-variables/71121/9 "2022-11-23T17:24:42Z")

</div>

> [@marcus-j-davies](#):
>
> `JSON.parse(JSON.stringify(OriginalObject))` whilst works, can get into trouble with circular references, may also be not as performant, having to construct a string before it can create another object.

And can also lose properties that don't survive the round-trip to JSON and back again. For example, any `Date` objects.

---

<div class="post-metadata">

### Author: ![DavePF](https://avatars.discourse-cdn.com/v4/letter/d/48db29/32.png) [@DavePF](https://discourse.nodered.org/u/DavePF)
#### Post date: [23 November 2022 18:10 UTC](https://discourse.nodered.org/t/issues-with-variables/71121/10 "2022-11-23T18:10:08Z")

</div>

Greatly appreciated ! Thanks !

---

<div class="post-metadata">

### Author: ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)
#### Post date: [23 November 2022 19:56 UTC](https://discourse.nodered.org/t/issues-with-variables/71121/11 "2022-11-23T19:56:18Z")

</div>

> [@DavePF](#):
>
> What would be the best practice way of making a deep copy ?

I don't think cloneMessage is a deep copy. JSON stringify/parse, as Nick says isn't always type safe and indeed may well fail - always wrap in a `try` block.

True deep copies in JavaScript are not simple. But there is plenty written online about the subject.

> [@Colin](#):
>
> I am not sure about that, at least not with the standard file storage context. At run time it is still in memory, it is just saved to disc occasionally and restored on restart.

That's why I said "some". 🙂 It very much depends on the mechanism used.

---

<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: [7 December 2022 19:56 UTC](https://discourse.nodered.org/t/issues-with-variables/71121/12 "2022-12-07T19:56:26Z")

</div>

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