From my other post:
This one
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.
[{"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"]]}]