# Storing data in flow without using flow.set (array of objects)

**URL:** <https://discourse.nodered.org/t/storing-data-in-flow-without-using-flow-set-array-of-objects/16461>\
**Category:** General\
**Created:** [8 October 2019 09:38 UTC](https://discourse.nodered.org/t/storing-data-in-flow-without-using-flow-set-array-of-objects/16461 "2019-10-08T09:38:43Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![homeplayer](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/homeplayer/32/12388_2.png) [@homeplayer](https://discourse.nodered.org/u/homeplayer)\
**Post date:** [8 October 2019 09:38 UTC](https://discourse.nodered.org/t/storing-data-in-flow-without-using-flow-set-array-of-objects/16461/1 "2019-10-08T09:38:43Z")

</div>

I am a noob when it comes to programming, See me as a Frankenstein coder.

The data stored in flow data =

```auto
[
{"ps":"previous state","type":"1","hold":"0","count":0,"name":"name","resrv":0},
{"ps":false,"type":0,"hold":0,"count":0,"name":"name","resrv":0},
{"ps":false,"type":0,"hold":0,"count":0,"name":"name","resrv":0}
.... and some more of the same]

```

If i run the following code

```auto
var y = flow.get('data') || 0;

y[1].name = "this";
//flow.set('data',y);

msg.test = flow.get('data['+1+'].name');
return msg;

```

the output is - **test: "this"** where it should be **"name"**  
and it is stored in flow data.

But when I try this with [Steve’s Internet Guide -- Storing Data in Node-Red Variables](http://www.steves-internet-guide.com/node-red-variables/)  
(the function is edit by me)

```auto
//var count=context.get('count') || 0;
var count = flow.get('count') || 0;

count = 8;
msg.payload=flow.get('count');
//flow.set('count',count);
return msg;

```

it will not set the flow variable.

is it because of the variable type array of objects and the reference to it  
please explain thanks

edit added the flow

```auto
[{"id":"2b25bfd6.19345","type":"function","z":"3666b531.e1652a","name":"","func":"\nvar y = flow.get('data') || 0;\n\ny[1].name = \"this\";\n//flow.set('data',y);\n\nmsg.test = flow.get('data['+1+'].name')\n\nvar i =2;\n//flow.set('data['+i+'].name',\"ikke\")\n\nreturn msg;\n\n\n","outputs":1,"noerr":0,"x":490,"y":260,"wires":[["b005f2d3.2bc1e"]]},{"id":"f24dc261.54a55","type":"inject","z":"3666b531.e1652a","name":"Items list","topic":"","payload":"[{\"ps\":\"previous state\",\"type\":\"1,2,3,4,5,6\",\"hold\":\"0,1,2\",\"count\":0,\"name\":\"name\",\"resrv\":0},{\"ps\":false,\"type\":0,\"hold\":0,\"count\":0,\"name\":\"name\",\"resrv\":0},{\"ps\":false,\"type\":0,\"hold\":0,\"count\":0,\"name\":\"name\",\"resrv\":0},{\"ps\":false,\"type\":0,\"hold\":0,\"count\":0,\"name\":\"name\",\"resrv\":0},{\"ps\":false,\"type\":0,\"hold\":0,\"count\":0,\"name\":\"name\",\"resrv\":0},{\"ps\":false,\"type\":0,\"hold\":0,\"count\":0,\"name\":\"name\",\"resrv\":0},{\"ps\":false,\"type\":0,\"hold\":0,\"count\":0,\"name\":\"name\",\"resrv\":0},{\"ps\":false,\"type\":0,\"hold\":0,\"count\":0,\"name\":\"name\",\"resrv\":0},{\"ps\":false,\"type\":0,\"hold\":0,\"count\":0,\"name\":\"name\",\"resrv\":0},{\"ps\":false,\"type\":0,\"hold\":0,\"count\":0,\"name\":\"name\",\"resrv\":0},{\"ps\":false,\"type\":0,\"hold\":0,\"count\":0,\"name\":\"name\",\"resrv\":0},{\"ps\":false,\"type\":0,\"hold\":0,\"count\":0,\"name\":\"name\",\"resrv\":0},{\"ps\":false,\"type\":0,\"hold\":0,\"count\":0,\"name\":\"name\",\"resrv\":0},{\"ps\":false,\"type\":0,\"hold\":0,\"count\":0,\"name\":\"name\",\"resrv\":0},{\"ps\":false,\"type\":0,\"hold\":0,\"count\":0,\"name\":\"name\",\"resrv\":0},{\"ps\":false,\"type\":0,\"hold\":0,\"count\":0,\"name\":\"name\",\"resrv\":0},{\"ps\":false,\"type\":0,\"hold\":0,\"count\":0,\"name\":\"name\",\"resrv\":0},{\"ps\":false,\"type\":0,\"hold\":0,\"count\":0,\"name\":\"name\",\"resrv\":0},{\"ps\":false,\"type\":0,\"hold\":0,\"count\":0,\"name\":\"name\",\"resrv\":0},{\"ps\":false,\"type\":0,\"hold\":0,\"count\":0,\"name\":\"name\",\"resrv\":0},{\"ps\":false,\"type\":0,\"hold\":0,\"count\":0,\"name\":\"name\",\"resrv\":0},{\"ps\":false,\"type\":0,\"hold\":0,\"count\":0,\"name\":\"name\",\"resrv\":0},{\"ps\":false,\"type\":0,\"hold\":0,\"count\":0,\"name\":\"name\",\"resrv\":0},{\"ps\":false,\"type\":0,\"hold\":0,\"count\":0,\"name\":\"name\",\"resrv\":0},{\"ps\":false,\"type\":0,\"hold\":0,\"count\":0,\"name\":\"name\",\"resrv\":0},{\"ps\":false,\"type\":0,\"hold\":0,\"count\":0,\"name\":\"name\",\"resrv\":0},{\"ps\":false,\"type\":0,\"hold\":0,\"count\":0,\"name\":\"name\",\"resrv\":0},{\"ps\":false,\"type\":0,\"hold\":0,\"count\":0,\"name\":\"name\",\"resrv\":0},{\"ps\":false,\"type\":0,\"hold\":0,\"count\":0,\"name\":\"name\",\"resrv\":0},{\"ps\":false,\"type\":0,\"hold\":0,\"count\":0,\"name\":\"name\",\"resrv\":0},{\"ps\":false,\"type\":0,\"hold\":0,\"count\":0,\"name\":\"name\",\"resrv\":0},{\"ps\":false,\"type\":0,\"hold\":0,\"count\":0,\"name\":\"name\",\"resrv\":0},{\"ps\":false,\"type\":0,\"hold\":0,\"count\":0,\"name\":\"name\",\"resrv\":0},{\"ps\":false,\"type\":0,\"hold\":0,\"count\":0,\"name\":\"name\",\"resrv\":0},{\"ps\":false,\"type\":0,\"hold\":0,\"count\":0,\"name\":\"name\",\"resrv\":0},{\"ps\":false,\"type\":0,\"hold\":0,\"count\":0,\"name\":\"name\",\"resrv\":0},{\"ps\":false,\"type\":0,\"hold\":0,\"count\":0,\"name\":\"name\",\"resrv\":0},{\"ps\":false,\"type\":0,\"hold\":0,\"count\":0,\"name\":\"name\",\"resrv\":0},{\"ps\":false,\"type\":0,\"hold\":0,\"count\":0,\"name\":\"name\",\"resrv\":0},{\"ps\":false,\"type\":0,\"hold\":0,\"count\":0,\"name\":\"name\",\"resrv\":0},{\"ps\":false,\"type\":0,\"hold\":0,\"count\":0,\"name\":\"name\",\"resrv\":0},{\"ps\":false,\"type\":0,\"hold\":0,\"count\":0,\"name\":\"name\",\"resrv\":0},{\"ps\":false,\"type\":0,\"hold\":0,\"count\":0,\"name\":\"name\",\"resrv\":0},{\"ps\":false,\"type\":0,\"hold\":0,\"count\":0,\"name\":\"name\",\"resrv\":0},{\"ps\":false,\"type\":0,\"hold\":0,\"count\":0,\"name\":\"name\",\"resrv\":0},{\"ps\":false,\"type\":0,\"hold\":0,\"count\":0,\"name\":\"name\",\"resrv\":0},{\"ps\":false,\"type\":0,\"hold\":0,\"count\":0,\"name\":\"name\",\"resrv\":0},{\"ps\":false,\"type\":0,\"hold\":0,\"count\":0,\"name\":\"name\",\"resrv\":0}]","payloadType":"json","repeat":"","crontab":"","once":true,"onceDelay":0.1,"x":200,"y":260,"wires":[["5375937e.8dc7ec"]]},{"id":"b005f2d3.2bc1e","type":"debug","z":"3666b531.e1652a","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","x":630,"y":260,"wires":[]},{"id":"5375937e.8dc7ec","type":"function","z":"3666b531.e1652a","name":"","func":"var test=flow.get('data') || 0;\n\nif (test ===0){\n flow.set('data',msg.payload);\n}\nmsg.payload = \"\";\nreturn msg;","outputs":1,"noerr":0,"x":370,"y":260,"wires":[["2b25bfd6.19345"]]}]

```

---

<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:** [8 October 2019 09:46 UTC](https://discourse.nodered.org/t/storing-data-in-flow-without-using-flow-set-array-of-objects/16461/2 "2019-10-08T09:46:21Z")

</div>

> [@homeplayer](#):
>
> ```auto
> //var count=context.get('count') || 0;
> var count = flow.get('count') || 0;
> 
> count = 8;
> msg.payload=flow.get('count');
> //flow.set('count',count);
> return msg;
> 
> ```
> 
> it will not set the flow variable.

and where in this code are you setting the flow variable?

---

<div class="post-metadata">

**Author:** ![homeplayer](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/homeplayer/32/12388_2.png) [@homeplayer](https://discourse.nodered.org/u/homeplayer)\
**Post date:** [8 October 2019 09:48 UTC](https://discourse.nodered.org/t/storing-data-in-flow-without-using-flow-set-array-of-objects/16461/3 "2019-10-08T09:48:12Z")

</div>

see op, added the flow at the bottom

---

<div class="post-metadata">

**Author:** ![cymplecy](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/cymplecy/32/2773_2.png) [@cymplecy](https://discourse.nodered.org/u/cymplecy)\
**Post date:** [8 October 2019 10:59 UTC](https://discourse.nodered.org/t/storing-data-in-flow-without-using-flow-set-array-of-objects/16461/4 "2019-10-08T10:59:53Z")

</div>

```auto
var y = flow.get('data') || 0;

```

You are setting y to be the same as flow.data - since it is an array, y will be the same object and NOT a copy of flow.data

```auto
y[1].name = "this";
//flow.set('data',y);

```

you are changing y so you are changing flow.data as well

```auto
msg.test = flow.get('data['+1+'].name');

```

You are setting msg.test to be flow.data[1].name which will be "this"

```auto
return msg;

```

---

<div class="post-metadata">

**Author:** ![homeplayer](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/homeplayer/32/12388_2.png) [@homeplayer](https://discourse.nodered.org/u/homeplayer)\
**Post date:** [8 October 2019 11:43 UTC](https://discourse.nodered.org/t/storing-data-in-flow-without-using-flow-set-array-of-objects/16461/5 "2019-10-08T11:43:55Z")

</div>

> [@cymplecy](#):
>
> ```auto
> var y = flow.get('data') || 0;
> 
> ```
> 
> You are setting y to be the same as flow.data - it will be the same object and NOT a copy of flow.data

```auto
//var count=context.get('count') || 0; var count = flow.get('count') || 0; count = 8;

```

so why isn't it the same whit count?  
changing count = 8;

```auto
count = 8;

```

---

<div class="post-metadata">

**Author:** ![cymplecy](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/cymplecy/32/2773_2.png) [@cymplecy](https://discourse.nodered.org/u/cymplecy)\
**Post date:** [8 October 2019 11:47 UTC](https://discourse.nodered.org/t/storing-data-in-flow-without-using-flow-set-array-of-objects/16461/6 "2019-10-08T11:47:57Z")

</div>

```auto
var count = flow.get('count') || 0;

```

count set to be same as flow.count(If flow.count is an array or an object). If flow.count is a simple number or string, then count will have a copy of that value or string but will not be the same object

```auto
count = 8;

```

count now set to be 8. - it is no longer a reference to flow.count (if it ever was in the 1st place)

```auto
msg.payload=flow.get('count');

```

payload set to be flow.count

```auto
return msg;

```

---

<div class="post-metadata">

**Author:** ![cymplecy](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/cymplecy/32/2773_2.png) [@cymplecy](https://discourse.nodered.org/u/cymplecy)\
**Post date:** [8 October 2019 12:23 UTC](https://discourse.nodered.org/t/storing-data-in-flow-without-using-flow-set-array-of-objects/16461/7 "2019-10-08T12:23:58Z")

</div>

I've added some more info but its a complicated subject 🙂  
This might help

- Changing the value of a variable _never_ changes the underlying primitive or object, it just points the variable to a new primitive or object.
- However, changing a _property_ of an object referenced by a variable does change the underlying object.

> <https://stackoverflow.com/questions/6605640/javascript-by-reference-vs-by-value>
