Reset an array in a function node


I found this code on this forum

var myArray = context.get("myArray");

if (!myArray) {
    console.log("Set the array on context memory");
    context.set("myArray", []);
    myArray = context.get("myArray");

msg.payload = myArray;
return msg;

And it works like it should, but how do I reset the array?
Reason: I want to collect data once a day for a month, then reset, and start on a new month

Use something in the msg to signal you want to reset the array. E.g. test for msg.topic == "reset" then clear the array.

Yep. And then to clear the array, just set it to an empty array value.

myArray = [];
context.set('myArray', []);

You've got it from there.

Thanks, but this only added an timestamp to the arryay……?


Yes. That's being caused by your inject node pushing a timestamp as msg and your function adding it near the end.


Keep that group you already have as your normal function. But put in an if condition that runs that normally or just clears the array of it receives some kind of reset message like Steve said. Or remove the msg.payload from your inject node completely and instead use a topic or some other key for a message. That way you change nothing in your code while testing.

I have tried you suggestions, but I'm reaaly not able to clear the array, please see my nodes here:

[{"id":"d394740851723e6d","type":"inject","z":"c733ee6b707eabb8","name":"Test","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":680,"y":930,"wires":[["f3311e99338ebf39"]]},{"id":"f3311e99338ebf39","type":"function","z":"c733ee6b707eabb8","name":"highest_consumption_last_day","func":"flow.set(\"highest_consumption_last_day\", msg.payload)\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":940,"y":990,"wires":[["1c496feef15e7e2a","e174335d8e9cde50"]]},{"id":"e174335d8e9cde50","type":"function","z":"c733ee6b707eabb8","name":"function 17","func":"var myArray = context.get(\"myArray\");\n\nif (!myArray) {\n    console.log(\"Set the array on context memory\");\n    context.set(\"myArray\", []);\n    myArray = context.get(\"myArray\");\n}\n\nmyArray.push(msg.payload);\nmsg.payload = myArray;\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":1180,"y":940,"wires":[["8c40a6039770b3d2"]]},{"id":"8c40a6039770b3d2","type":"debug","z":"c733ee6b707eabb8","name":"debug 79","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":1340,"y":940,"wires":[]},{"id":"4ac84c3d12274325","type":"function","z":"c733ee6b707eabb8","name":"function 18","func":"context.set('myArray', []);\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":860,"y":1170,"wires":[["e174335d8e9cde50"]]},{"id":"c06bb7f26bf6d2bd","type":"inject","z":"c733ee6b707eabb8","name":"","props":[{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"reset","x":680,"y":1170,"wires":[["4ac84c3d12274325"]]}]

In order to make code readable and usable it is necessary to surround your code with three backticks (also known as a left quote or backquote ```)

   code goes here 

You can edit and correct your post by clicking the pencil :pencil2: icon.

See this post for more details - How to share code or flow json

Sorry, it's updated now

Remove the wire between Function 18 and Function 17. The effect of that wire is to pass msg.payload through to function 17, which adds the payload to your empty array.

No, thats not what I said.

I said...



let myArray = context.get("myArray");

if (msg.topic === 'reset') {
    context.set("myArray", [])
    msg.payload = []
    return msg

if (!myArray) {
    console.log("Set the array on context memory");
    myArray = []
    context.set("myArray", myArray );

msg.payload = myArray;
return msg;
1 Like

Perfect ! Thank you very much

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