# Reset an array in a function node

**URL:** https://discourse.nodered.org/t/reset-an-array-in-a-function-node/68952
**Category:** General
**Created:** [12 October 2022 22:07 UTC](https://discourse.nodered.org/t/reset-an-array-in-a-function-node/68952 "2022-10-12T22:07:16Z")
**Posts on this page:** 12
**Page:** 1

<div class="post-metadata">

### Author: ![PowerZone21](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/powerzone21/32/67873_2.png) [@PowerZone21](https://discourse.nodered.org/u/PowerZone21)
#### Post date: [12 October 2022 22:07 UTC](https://discourse.nodered.org/t/reset-an-array-in-a-function-node/68952/1 "2022-10-12T22:07:16Z")

</div>

Hi,

I found this code on this forum

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

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

myArray.push(msg.payload);
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

---

<div class="post-metadata">

### Author: ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)
#### Post date: [12 October 2022 22:42 UTC](https://discourse.nodered.org/t/reset-an-array-in-a-function-node/68952/2 "2022-10-12T22:42:19Z")

</div>

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

---

<div class="post-metadata">

### Author: ![madhouse](https://avatars.discourse-cdn.com/v4/letter/m/b77776/32.png) [@madhouse](https://discourse.nodered.org/u/madhouse)
#### Post date: [13 October 2022 02:31 UTC](https://discourse.nodered.org/t/reset-an-array-in-a-function-node/68952/3 "2022-10-13T02:31:34Z")

</div>

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

```auto
myArray = [];
//or
context.set('myArray', []);

```

You've got it from there.

---

<div class="post-metadata">

### Author: ![PowerZone21](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/powerzone21/32/67873_2.png) [@PowerZone21](https://discourse.nodered.org/u/PowerZone21)
#### Post date: [13 October 2022 04:44 UTC](https://discourse.nodered.org/t/reset-an-array-in-a-function-node/68952/4 "2022-10-13T04:44:35Z")

</div>

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

Regards

---

<div class="post-metadata">

### Author: ![madhouse](https://avatars.discourse-cdn.com/v4/letter/m/b77776/32.png) [@madhouse](https://discourse.nodered.org/u/madhouse)
#### Post date: [13 October 2022 05:20 UTC](https://discourse.nodered.org/t/reset-an-array-in-a-function-node/68952/5 "2022-10-13T05:20:23Z")

</div>

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

```auto
myArray.push(msg.payload);

```

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.

---

<div class="post-metadata">

### Author: ![PowerZone21](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/powerzone21/32/67873_2.png) [@PowerZone21](https://discourse.nodered.org/u/PowerZone21)
#### Post date: [13 October 2022 06:25 UTC](https://discourse.nodered.org/t/reset-an-array-in-a-function-node/68952/6 "2022-10-13T06:25:53Z")

</div>

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

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

```

---

<div class="post-metadata">

### Author: ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)
#### Post date: [13 October 2022 06:34 UTC](https://discourse.nodered.org/t/reset-an-array-in-a-function-node/68952/7 "2022-10-13T06:34:10Z")

</div>

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 `````)

````auto
``` 
   code goes here 
```

````

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

See this post for more details - [How to share code or flow json](https://discourse.nodered.org/t/how-to-share-code-or-flow-json/506)

---

<div class="post-metadata">

### Author: ![PowerZone21](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/powerzone21/32/67873_2.png) [@PowerZone21](https://discourse.nodered.org/u/PowerZone21)
#### Post date: [13 October 2022 09:11 UTC](https://discourse.nodered.org/t/reset-an-array-in-a-function-node/68952/8 "2022-10-13T09:11:30Z")

</div>

Sorry, it's updated now

---

<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: [13 October 2022 09:25 UTC](https://discourse.nodered.org/t/reset-an-array-in-a-function-node/68952/9 "2022-10-13T09:25:59Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)
#### Post date: [13 October 2022 09:59 UTC](https://discourse.nodered.org/t/reset-an-array-in-a-function-node/68952/10 "2022-10-13T09:59:29Z")

</div>

No, thats not what I said.

I said...

> [@Steve-Mcl](#):
>
> Use something in the msg to signal you want to reset the array. E.g. test for msg.topic == "reset" then clear the array

e.g...

![chrome_gshcjX8xAx](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/7/b/7bb34b422177b9b144bb90f6d6ae2e887db857f3.gif)

```auto
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 );
}

myArray.push(msg.payload);
msg.payload = myArray;
return msg;

```

---

<div class="post-metadata">

### Author: ![PowerZone21](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/powerzone21/32/67873_2.png) [@PowerZone21](https://discourse.nodered.org/u/PowerZone21)
#### Post date: [13 October 2022 10:11 UTC](https://discourse.nodered.org/t/reset-an-array-in-a-function-node/68952/11 "2022-10-13T10:11:29Z")

</div>

> [@Steve-Mcl](#):
>
> ```auto
> 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 );
> }
> 
> myArray.push(msg.payload);
> msg.payload = myArray;
> return msg;
> 
> ```

Perfect ! Thank you very much

---

<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: [27 October 2022 10:11 UTC](https://discourse.nodered.org/t/reset-an-array-in-a-function-node/68952/12 "2022-10-27T10:11:56Z")

</div>

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