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

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

The data stored in flow data =

[
{"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

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
(the function is edit by me)

//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

[{"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"]]}]

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

see op, added the flow at the bottom

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

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

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

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

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

return msg;
//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;

count = 8;
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

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)

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

payload set to be flow.count

return msg;

I've added some more info but its a complicated subject :slight_smile:
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.