# How to create an array, list individual entries and delete slected items

**URL:** <https://discourse.nodered.org/t/how-to-create-an-array-list-individual-entries-and-delete-slected-items/20621>\
**Category:** Share Your Projects\
**Created:** [20 January 2020 11:35 UTC](https://discourse.nodered.org/t/how-to-create-an-array-list-individual-entries-and-delete-slected-items/20621 "2020-01-20T11:35:47Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)\
**Post date:** [20 January 2020 11:35 UTC](https://discourse.nodered.org/t/how-to-create-an-array-list-individual-entries-and-delete-slected-items/20621/1 "2020-01-20T11:35:47Z")

</div>

From my other post:  
[This one](https://discourse.nodered.org/t/message-queue-node-that-can-be-indexed/20555/29)

This is a working example of how to make an _array_ and be able to step through it. Forward and Backward.

You can delete individual entries or all.

The message generation is rather basic, but suffice for the needs of the flow.

To generate the entries: press the `add new item to array` node. (Top left)  
Below that are the _Next_ and _Previous_ buttons.  
It displays the OLDEST FIRST. And because I am not that smart, if you press the _Next_ button you are shown the second entry.

Click the _Previous_ to see the first one.  
(You could say it is a bug)

Generate a few entries and look at them in the `context` menu and refresh the `flow` values.

Then scroll to an arbitrary entry and press the _Wipe_ button.  
That will be deleted from the array.

Just a bit of fun if feel like it.

```auto
[{"id":"638c9635.68a788","type":"inject","z":"184dc884.7aba5f","name":"Add new item to array","topic":"","payload":"blah_","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":190,"y":1220,"wires":[["26162964.640a16"]]},{"id":"26162964.640a16","type":"random","z":"184dc884.7aba5f","name":"Random number","low":"1","high":"10","inte":"true","property":"payload","x":200,"y":1250,"wires":[["c0ef557b.139f1"]]},{"id":"c0ef557b.139f1","type":"template","z":"184dc884.7aba5f","name":"","field":"payload","fieldType":"msg","format":"handlebars","syntax":"mustache","template":"Message {{payload}}","output":"str","x":230,"y":1280,"wires":[["8e458e68.2f8d88","fcb9dca0.971b5"]]},{"id":"8e458e68.2f8d88","type":"function","z":"184dc884.7aba5f","name":"Array","func":"//\nvar output = \"\"; // This is what returns the indexed value.\nvar i = context.get('pointer') || 0;\nvar j = 0;\nvar queue_array = flow.get(\"queue\") // I'm guessing this checks if the array exists.\nif (!Array.isArray(queue_array))\n{\n // If not, set up array.\n node.warn(\"Initialising array\");\n queue_array = [];\n flow.set('queue',queue_array);\n node.warn(\"Array set up.\");\n}\n\nsize = queue_array.length;\n\nif (msg.topic == \"CONTROL\")\n{\n // Stuff to be done with array.\n if (msg.payload == \"NEXT\")\n {\n // Display next item\n i = i + 1;\n j = i + 1;\n node.warn(\"Next entry \" + (i+1));\n output = queue_array[i];\n msg.payload = output;\n context.set('pointer',i); // Save the value.\n node.status(size + \" \" + (i+1));\n return msg;\n } else\n if (msg.payload == \"PREVIOUS\")\n {\n // Display previous item\n i = i - 1;\n j = i + 1;\n node.warn(\"Previous entry \" + (i+1));\n output = queue_array[i];\n msg.payload = output;\n context.set('pointer',i); // Save the value.\n node.status(size + \" \" + (i+1));\n return msg;\n } else\n if (msg.payload == \"WIPE\")\n {\n // Delete this item\n j = i + 1;\n node.warn(\"Removing item \" + (i+1) + \" which is \" + queue_array[i]);\n queue_array.splice(i,1);\n flow.set('queue', queue_array);\n return;\n //\n //\n } else\n if (msg.payload == \"WIPE ALL\")\n {\n // Wipe the whole array\n node.warn(\"Initialising array\");\n queue_array = [];\n flow.set('queue',queue_array);\n node.warn(\"Array Wiped.\");\n return;\n }\n}\n\nnode.warn(\"saving data to array\");\n\nqueue_array.push(msg.payload);\n\nsize = queue_array.length;\n\ncontext.set('pointer',i); // Save the value - for what ever reason.\n\nflow.set('queue',queue_array);\nnode.status(size + \" \" + i);\n\nreturn;\n","outputs":1,"noerr":0,"x":490,"y":1320,"wires":[["e09fc19c.cf203"]]},{"id":"fcb9dca0.971b5","type":"debug","z":"184dc884.7aba5f","name":"History","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","x":500,"y":1280,"wires":[]},{"id":"e09fc19c.cf203","type":"debug","z":"184dc884.7aba5f","name":"Index result","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","x":650,"y":1320,"wires":[]},{"id":"c4bbdffc.81ab78","type":"inject","z":"184dc884.7aba5f","name":"Next","topic":"CONTROL","payload":"NEXT","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":240,"y":1360,"wires":[["8e458e68.2f8d88"]]},{"id":"16235d1e.a6c2db","type":"inject","z":"184dc884.7aba5f","name":"Previous","topic":"CONTROL","payload":"PREVIOUS","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":230,"y":1390,"wires":[["8e458e68.2f8d88"]]},{"id":"8d565471.b8f5a","type":"inject","z":"184dc884.7aba5f","name":"Wipe All","topic":"CONTROL","payload":"WIPE ALL","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":230,"y":1470,"wires":[["8e458e68.2f8d88"]]},{"id":"bb6554dd.38b188","type":"inject","z":"184dc884.7aba5f","name":"Wipe","topic":"CONTROL","payload":"WIPE","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":240,"y":1440,"wires":[["8e458e68.2f8d88"]]}]

```

---

<div class="post-metadata">

**Author:** ![hotNipi](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/hotnipi/32/383_2.png) [@hotNipi](https://discourse.nodered.org/u/hotNipi)\
**Post date:** [20 January 2020 11:46 UTC](https://discourse.nodered.org/t/how-to-create-an-array-list-individual-entries-and-delete-slected-items/20621/2 "2020-01-20T11:46:29Z")

</div>

Slight additions maybe (to make it more stable)  
At any time you change the pointer value (increase or decrease) make sure it still fits into boundaries.  
Boundaries are:  
0 (zero) for low end  
array.length -1 for high end.

---

<div class="post-metadata">

**Author:** ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)\
**Post date:** [21 January 2020 07:00 UTC](https://discourse.nodered.org/t/how-to-create-an-array-list-individual-entries-and-delete-slected-items/20621/3 "2020-01-21T07:00:39Z")

</div>

This is the flow modified.

If you are at the first entry and press the _previous_ button, you will get a message saying `end limit`

```auto
[{"id":"24d3ff30.ed2678","type":"function","z":"184dc884.7aba5f","name":"Array","func":"//\nvar output = \"\"; // This is what returns the indexed value.\nvar i = context.get('pointer') || 0;\nvar queue_array = flow.get(\"queue\") // I'm guessing this checks if the array exists.\nif (!Array.isArray(queue_array))\n{\n // If not, set up array.\n node.warn(\"Initialising array\");\n queue_array = [];\n flow.set('queue',queue_array);\n node.warn(\"Array set up.\");\n}\n\nsize = queue_array.length;\n\nif (msg.topic == \"CONTROL\")\n{\n // Stuff to be done with array.\n if (msg.payload == \"NEXT\")\n {\n // Display next item\n if (i < size)\n {\n i = i + 1;\n } else\n {\n msg.payload = \"End limit\";\n return msg;\n }\n node.warn(\"Next entry \" + i);\n output = queue_array[(i-1)];\n context.set('pointer',i); // Save the value.\n node.status(size + \" \" + i);\n msg.payload = output;\n return msg;\n } else\n if (msg.payload == \"PREVIOUS\")\n {\n // Display previous item\n if (i > 1)\n {\n i = i - 1;\n } else\n {\n msg.payload = \"End limit\";\n return msg;\n }\n node.warn(\"Previous entry \" + i);\n output = queue_array[(i-1)];\n context.set('pointer',i); // Save the value.\n node.status(size + \" \" + i);\n msg.payload = output;\n return msg;\n } else\n if (msg.payload == \"WIPE\")\n {\n // Delete this item\n node.warn(\"Removing item \" + (i-1) + \" which is \" + queue_array[i]);\n queue_array.splice(i,1);\n flow.set('queue', queue_array);\n return;\n //\n //\n } else\n if (msg.payload == \"WIPE ALL\")\n {\n // Wipe the whole array\n node.warn(\"Initialising array\");\n queue_array = [];\n flow.set('queue',queue_array);\n node.warn(\"Array Wiped.\");\n return;\n }\n}\n\nnode.warn(\"saving data to array\");\n\nqueue_array.push(msg.payload);\n\nsize = queue_array.length;\n\ncontext.set('pointer',i); // Save the value - for what ever reason.\n\nflow.set('queue',queue_array);\nnode.status(size + \" \" + i);\n\nreturn;\n","outputs":1,"noerr":0,"x":790,"y":230,"wires":[["bb81c8ee.378dc8"]]},{"id":"e6bfa23d.3af5c","type":"template","z":"184dc884.7aba5f","name":"","field":"payload","fieldType":"msg","format":"handlebars","syntax":"mustache","template":"Message {{payload}}","output":"str","x":530,"y":190,"wires":[["24d3ff30.ed2678","79795f30.8f96b"]]},{"id":"bb81c8ee.378dc8","type":"debug","z":"184dc884.7aba5f","name":"Index result","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","x":950,"y":230,"wires":[]},{"id":"6114d1e4.d465f","type":"inject","z":"184dc884.7aba5f","name":"Next","topic":"CONTROL","payload":"NEXT","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":540,"y":270,"wires":[["24d3ff30.ed2678"]]},{"id":"65348b56.b8890c","type":"inject","z":"184dc884.7aba5f","name":"Previous","topic":"CONTROL","payload":"PREVIOUS","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":530,"y":300,"wires":[["24d3ff30.ed2678"]]},{"id":"86bff95.947a088","type":"inject","z":"184dc884.7aba5f","name":"Wipe","topic":"CONTROL","payload":"WIPE","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":540,"y":350,"wires":[["24d3ff30.ed2678"]]},{"id":"db02aab3.b25a18","type":"inject","z":"184dc884.7aba5f","name":"Wipe All","topic":"CONTROL","payload":"WIPE ALL","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":530,"y":380,"wires":[["24d3ff30.ed2678"]]},{"id":"bcd9c3f1.12907","type":"random","z":"184dc884.7aba5f","name":"Random number","low":"1","high":"10","inte":"true","property":"payload","x":500,"y":160,"wires":[["e6bfa23d.3af5c"]]},{"id":"79795f30.8f96b","type":"debug","z":"184dc884.7aba5f","name":"History","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","x":800,"y":190,"wires":[]},{"id":"4f8b5d10.34e504","type":"inject","z":"184dc884.7aba5f","name":"Add new item to array","topic":"","payload":"blah_","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":490,"y":130,"wires":[["bcd9c3f1.12907"]]}]

```
