How to add to array elements?

Could you help me to add an element/elements to the array and then display array in the debug window.
I do something wrong!

var array = flow.get('array') || "";

flow.set("array", flow.get("array").push(msg.payload));
return [ msg ];

my flow

[{"id":"8e05673f.25a118","type":"tab","label":"Delay","disabled":false,"info":""},{"id":"e9887f38.771fb","type":"inject","z":"8e05673f.25a118","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"100","payloadType":"num","x":170,"y":200,"wires":[["8f92e447.ec7758"]]},{"id":"8f92e447.ec7758","type":"function","z":"8e05673f.25a118","name":"","func":"var array = flow.get('array') || \"\";\n\nflow.set(\"array\", flow.get(\"array\").push(msg.payload));\nreturn [ msg ];","outputs":1,"noerr":0,"initialize":"","finalize":"","x":400,"y":240,"wires":[["48b4aadb.221d54","f70ea5c3.3a8408"]]},{"id":"f70ea5c3.3a8408","type":"ui_slider","z":"8e05673f.25a118","name":"Brightness","label":"Яркость","tooltip":"","group":"bf8577b0.734c08","order":2,"width":0,"height":0,"passthru":true,"outs":"end","topic":"brightness","min":"1","max":"254","step":1,"x":690,"y":240,"wires":[[]]},{"id":"48b4aadb.221d54","type":"debug","z":"8e05673f.25a118","name":"2","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":410,"y":300,"wires":[]},{"id":"fb80fc91.c062f","type":"inject","z":"8e05673f.25a118","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"50","payloadType":"num","x":170,"y":240,"wires":[["8f92e447.ec7758"]]},{"id":"a10f32f8.65f5d","type":"inject","z":"8e05673f.25a118","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"0","payloadType":"num","x":170,"y":280,"wires":[["8f92e447.ec7758"]]},{"id":"db752fb9.2d235","type":"function","z":"8e05673f.25a118","name":"array","func":"var array = [];\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":390,"y":160,"wires":[[]]},{"id":"bf8577b0.734c08","type":"ui_group","name":"test","tab":"96dac147.c7906","order":1,"disp":true,"width":"6","collapse":false},{"id":"96dac147.c7906","type":"ui_tab","name":"Array","icon":"dashboard","order":6,"disabled":false,"hidden":false}]

Hi @zerg,

var array = flow.get('array') || "";

This line will mean, if array doesn't exist in context, you are returning a blank string - "", not an array.

flow.set("array", flow.get("array").push(msg.payload));

This line we store the result of calling flow.get("array").push(msg.payload) into context.
However, the Array.push function returns the length of the array - not the array itself, so that is what this line will be storing.

The correct version of your function is:

var array = flow.get('array') || [];
array.push(msg.payload);
flow.set("array", array);
return msg;
1 Like

Thank you for detailed explanation!
One more question about array, how to add elements to the array constantly? Because, current solution every time remove all elements from the array and after added a new element?
Question is: How to save "old" elements in array and every time add new elements?

fixed flow

[{"id":"8e05673f.25a118","type":"tab","label":"Delay","disabled":false,"info":""},{"id":"e9887f38.771fb","type":"inject","z":"8e05673f.25a118","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"100","payloadType":"num","x":170,"y":200,"wires":[["8f92e447.ec7758"]]},{"id":"8f92e447.ec7758","type":"function","z":"8e05673f.25a118","name":"","func":"var array = flow.get('array') || [];\narray.push(msg.payload);\nflow.set(\"array\", array);\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":400,"y":240,"wires":[["48b4aadb.221d54","f70ea5c3.3a8408"]]},{"id":"f70ea5c3.3a8408","type":"ui_slider","z":"8e05673f.25a118","name":"Brightness","label":"Яркость","tooltip":"","group":"bf8577b0.734c08","order":2,"width":0,"height":0,"passthru":true,"outs":"end","topic":"brightness","min":"1","max":"254","step":1,"x":690,"y":240,"wires":[[]]},{"id":"48b4aadb.221d54","type":"debug","z":"8e05673f.25a118","name":"2","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":410,"y":300,"wires":[]},{"id":"fb80fc91.c062f","type":"inject","z":"8e05673f.25a118","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"50","payloadType":"num","x":170,"y":240,"wires":[["8f92e447.ec7758"]]},{"id":"a10f32f8.65f5d","type":"inject","z":"8e05673f.25a118","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"0","payloadType":"num","x":170,"y":280,"wires":[["8f92e447.ec7758"]]},{"id":"bf8577b0.734c08","type":"ui_group","name":"test","tab":"96dac147.c7906","order":1,"disp":true,"width":"6","collapse":false},{"id":"96dac147.c7906","type":"ui_tab","name":"Array","icon":"dashboard","order":6,"disabled":false,"hidden":false}]

I don't understand what you mean by that. Do you mean that the array is cleared if you do a full Deploy or restart node red? If not that then what is it that clears the array?
If it is the deploy/restart problem you are worried about then you need to use persistent context so the values are retained over a restart. Working with context : Node-RED

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