For Each

Learn how to use For Each loop

When you need to iterate over an array, you can use the "For Each" loop under Programming section.

A For Each node has one input port and two output ports. Input port takes an array and starts iterating through the items. The upper output port injects each item until all the items are processed. After iterating all the items the flow continues from the below outport port.

The array to be iterated should be given to the For Each node's properties as below. The current item property will contain the iterated value.

When you finished processing the item through the the upper output port's branch, you have to connect a wire back to For Each node's input for the loop to work.

Or you can use a Go to Node and a Label to return to the For Each node's input as below:

You can copy-paste the following code into your flow board to try out the above example. Just Ctrl+c below code and then click into an empty region of your flowboard then press Ctrl+v.

{"nodes":[{"id":"cff0d973-7ec0-48fc-868a-8bfcd1f2f295","type":"simple","selected":true,"x":311,"y":198,"extras":{"inPayload":{"scope":"JS","name":"Date.now()"},"outPayload":{"scope":"Message","name":"payload"},"optRepeat":1,"optOnce":true,"optOnceDelay":0.1},"ports":[{"id":"0502974a-c86d-4158-bd14-6a3f9bb0483a","type":"default","selected":false,"name":"out_0502974a-c86d-4158-bd14-6a3f9bb0483a","parentNode":"cff0d973-7ec0-48fc-868a-8bfcd1f2f295","links":["0903a416-e50a-484b-a33d-278831ef0515"],"in":false,"label":"simple","position":"out"}],"model":{"id":"cff0d973-7ec0-48fc-868a-8bfcd1f2f295","name":"Inject","icon":"M10,9A1,1 0 0,1 11,8A1,1 0 0,1 12,9V13.47L13.21,13.6L18.15,15.79C18.68,16.03 19,16.56 19,17.14V21.5C18.97,22.32 18.32,22.97 17.5,23H11C10.62,23 10.26,22.85 10,22.57L5.1,18.37L5.84,17.6C6.03,17.39 6.3,17.28 6.58,17.28H6.8L10,19V9M11,5A4,4 0 0,1 15,9C15,10.5 14.2,11.77 13,12.46V11.24C13.61,10.69 14,9.89 14,9A3,3 0 0,0 11,6A3,3 0 0,0 8,9C8,9.89 8.39,10.69 9,11.24V12.46C7.8,11.77 7,10.5 7,9A4,4 0 0,1 11,5Z","color":"#FDD835","namespace":"Core.Trigger.Inject","version":"0.0.0","abc":[{},{"inPayload":{"scope":"JS","name":"Date.now()"}},{"outPayload":{"scope":"Message","name":"payload"}},{"optRepeat":1,"optOnce":true,"optOnceDelay":0.1}],"xyz":[{"schema":{"title":"Input","type":"object","properties":{"inPayload":{"type":"object","title":"Payload","variableType":"String","customScope":true,"jsScope":true,"argsScope":true,"properties":{"scope":{"type":"string"},"name":{"type":"string"}}}}},"uiSchema":{"inPayload":{"ui:field":"variable"}},"formData":{"inPayload":{"scope":"JS","name":"Date.now()"}}},{"schema":{"title":"Output","type":"object","properties":{"outPayload":{"type":"object","title":"Payload","variableType":"String","messageOnly":true,"properties":{"scope":{"type":"string"},"name":{"type":"string"}}}}},"uiSchema":{"outPayload":{"ui:field":"variable"}},"formData":{"outPayload":{"scope":"Message","name":"payload"}}},{"schema":{"title":"Options","type":"object","properties":{"optRepeat":{"title":"Repeat Interval (sec)","type":"number"},"optOnce":{"title":"Once","type":"boolean"},"optOnceDelay":{"title":"Once Delay (sec)","type":"number"}}},"uiSchema":{"ui:order":["optRepeat","optOnceDelay","optOnce"]},"formData":{"optRepeat":1,"optOnce":true,"optOnceDelay":0.1}}],"outputs":1,"inputs":0}},{"id":"331fc0bd-5871-4285-88db-7580b707c514","type":"simple","selected":true,"x":545,"y":200,"extras":{"outputs":1,"func":"msg.fruits=[\"Apple\", \"Orange\", \"Banana\"];\nreturn msg;\n","optTimeout":30,"variables":[]},"ports":[{"id":"3b52e96a-0c4c-4938-9e53-a26ba47ccdfd","type":"default","selected":false,"name":"out_3b52e96a-0c4c-4938-9e53-a26ba47ccdfd","parentNode":"331fc0bd-5871-4285-88db-7580b707c514","links":["23361e72-f40b-4b5f-a214-2b0717b24b68"],"in":false,"label":"simple","position":"out"},{"id":"a95fe994-b2b0-410b-8c7f-40e42ca56484","type":"default","selected":false,"name":"in","parentNode":"331fc0bd-5871-4285-88db-7580b707c514","links":["0903a416-e50a-484b-a33d-278831ef0515"],"in":true,"label":"simple","position":"in"}],"model":{"id":"331fc0bd-5871-4285-88db-7580b707c514","name":"Prepare Array","icon":"M15.6,5.29C14.5,5.19 13.53,6 13.43,7.11L13.18,10H16V12H13L12.56,17.07C12.37,19.27 10.43,20.9 8.23,20.7C6.92,20.59 5.82,19.86 5.17,18.83L6.67,17.33C6.91,18.07 7.57,18.64 8.4,18.71C9.5,18.81 10.47,18 10.57,16.89L11,12H8V10H11.17L11.44,6.93C11.63,4.73 13.57,3.1 15.77,3.3C17.08,3.41 18.18,4.14 18.83,5.17L17.33,6.67C17.09,5.93 16.43,5.36 15.6,5.29Z","color":"#F27925","editor":"javascript","namespace":"Core.Programming.Function","version":"0.0.0","abc":[{"name":"Prepare Array","color":"#F27925","delayBefore":0,"delayAfter":0,"continueOnError":false,"version":"0.0.0"},{"outputs":1,"func":"msg.fruits=[\"Apple\", \"Orange\", \"Banana\"];\nreturn msg;\n","optTimeout":30},{"variables":[]}],"xyz":[{"schema":{"title":"Options","type":"object","properties":{"func":{"title":"Function","type":"string"},"outputs":{"title":"Outputs","type":"number","minimum":1,"maximum":32},"optTimeout":{"title":"Script Timeout","type":"number"}}},"uiSchema":{"outputs":{"ui:widget":"updown"},"func":{"ui:widget":"hidden","ui:options":{"rows":6}}},"formData":{"outputs":1,"func":"msg.fruits=[\"Apple\", \"Orange\", \"Banana\"];\nreturn msg;\n","optTimeout":30}},{"schema":{"title":"Local Variables","type":"object","properties":{"variables":{"type":"array","items":{"type":"object","properties":{"name":{"type":"string"},"variableType":{"type":"string"}}}}}},"uiSchema":{"variables":{"ui:field":"localVariable"}},"formData":{"variables":[]}}],"outputs":1,"inputs":1}},{"id":"cb6f67ab-fe3d-41a1-936a-ddae33dde38e","type":"simple","selected":true,"x":815,"y":197,"extras":{"optInput":{"scope":"Message","name":"fruits"},"optOutput":{"scope":"Message","name":"fruit"}},"ports":[{"id":"3cd6f02f-4454-467c-90bc-74fc730bdddc","type":"default","selected":false,"name":"out_3cd6f02f-4454-467c-90bc-74fc730bdddc","parentNode":"cb6f67ab-fe3d-41a1-936a-ddae33dde38e","links":["7940da10-9a09-484f-bc71-ea273ec18e62","7e6f9e49-063a-4afb-956e-04c185687d0b"],"in":false,"label":"simple","position":"out"},{"id":"4f28ee95-c055-448d-9630-bf4b0517029c","type":"default","selected":false,"name":"out_4f28ee95-c055-448d-9630-bf4b0517029c","parentNode":"cb6f67ab-fe3d-41a1-936a-ddae33dde38e","links":["5f49c219-a822-49bb-a9ca-187fe7f8ef9d"],"in":false,"label":"simple","position":"out"},{"id":"208a17ba-9aba-423b-852a-a01c7988d6ad","type":"default","selected":false,"name":"in","parentNode":"cb6f67ab-fe3d-41a1-936a-ddae33dde38e","links":["23361e72-f40b-4b5f-a214-2b0717b24b68","a1235e82-2122-4b8e-b0c8-b6a0379a26f2"],"in":true,"label":"simple","position":"in"}],"model":{"id":"cb6f67ab-fe3d-41a1-936a-ddae33dde38e","name":"For Each Fruit","icon":"M13,3A9,9 0 0,0 4,12H1L4.89,15.89L4.96,16.03L9,12H6A7,7 0 0,1 13,5A7,7 0 0,1 20,12A7,7 0 0,1 13,19C11.07,19 9.32,18.21 8.06,16.94L6.64,18.36C8.27,20 10.5,21 13,21A9,9 0 0,0 22,12A9,9 0 0,0 13,3Z","color":"#b9aeec","namespace":"Core.Programming.ForEach","version":"0.0.0","abc":[{"name":"For Each Fruit","color":"#b9aeec","delayBefore":0,"delayAfter":0,"continueOnError":false,"version":"0.0.0"},{"optInput":{"scope":"Message","name":"fruits"},"optOutput":{"scope":"Message","name":"fruit"}}],"xyz":[{"schema":{"title":"Options","type":"object","properties":{"optInput":{"type":"object","title":"Loop through object","variableType":"Array","messageScope":true,"properties":{"scope":{"type":"string"},"name":{"type":"string"}}},"optOutput":{"type":"object","title":"Current Item","variableType":"string","messageScope":true,"messageOnly":true,"properties":{"scope":{"type":"string"},"name":{"type":"string"}}}}},"uiSchema":{"optInput":{"ui:field":"variable"},"optOutput":{"ui:field":"variable"}},"formData":{"optInput":{"scope":"Message","name":"fruits"},"optOutput":{"scope":"Message","name":"fruit"}}}],"outputs":2,"inputs":1}},{"id":"b99005a4-f27c-4cd4-88cf-c92970fcd319","type":"simple","selected":true,"x":1113,"y":195,"extras":{"optNodes":{"all":false,"ids":["89b8ff95-ad79-4199-9590-5ede96aaf599"]}},"ports":[{"id":"f274227a-306d-4270-bd2f-670aec448997","type":"default","selected":false,"name":"in","parentNode":"b99005a4-f27c-4cd4-88cf-c92970fcd319","links":["7e6f9e49-063a-4afb-956e-04c185687d0b"],"in":true,"label":"simple","position":"in"}],"model":{"id":"b99005a4-f27c-4cd4-88cf-c92970fcd319","name":"Go To Next Fruit","icon":"M11,16H3V8H11V2L21,12L11,22V16M13,7V10H5V14H13V17L18,12L13,7Z","color":"#444","namespace":"Core.Flow.GoTo","version":"0.0.0","abc":[{"name":"Go To Next Fruit","color":"#444","delayBefore":0,"delayAfter":0,"continueOnError":false,"version":"0.0.0"},{"optNodes":{"all":false,"ids":["89b8ff95-ad79-4199-9590-5ede96aaf599"]}}],"xyz":[{"schema":{"title":"Options","type":"object","inputs":1,"outputs":1,"properties":{"optNodes":{"type":"object","title":"","properties":{"ids":{"title":"","type":"array","items":{"type":"string"}},"all":{"title":"All","type":"boolean"}}}}},"uiSchema":{"optNodes":{"ui:field":"catch"},"ui:order":["optNodes"]},"formData":{"optNodes":{"all":false,"ids":["89b8ff95-ad79-4199-9590-5ede96aaf599"]}}}],"outputs":0,"inputs":1}},{"id":"89b8ff95-ad79-4199-9590-5ede96aaf599","type":"simple","selected":true,"x":556,"y":306,"extras":{},"ports":[{"id":"4b7786d8-6ba9-4c12-ad0d-f738db8d691a","type":"default","selected":false,"name":"out_4b7786d8-6ba9-4c12-ad0d-f738db8d691a","parentNode":"89b8ff95-ad79-4199-9590-5ede96aaf599","links":["a1235e82-2122-4b8e-b0c8-b6a0379a26f2"],"in":false,"label":"simple","position":"out"}],"model":{"id":"89b8ff95-ad79-4199-9590-5ede96aaf599","name":"Next Fruit","icon":"M11,16H3V8H11V2L21,12L11,22V16M13,7V10H5V14H13V17L18,12L13,7Z","color":"#444","namespace":"Core.Flow.Label","version":"0.0.0","abc":[{"name":"Next Fruit","color":"#444","delayBefore":0,"delayAfter":0,"continueOnError":false,"version":"0.0.0"}],"xyz":[],"outputs":1,"inputs":0}},{"id":"a17da7d8-7784-4138-8eac-d11cd7a87efc","type":"simple","selected":true,"x":1111,"y":112,"extras":{"optActive":true,"optSysConsole":false,"optDebugData":{"scope":"Message","name":"fruit"}},"ports":[{"id":"f29df236-54ec-4964-a2c4-b7139c7c23ff","type":"default","selected":false,"name":"in","parentNode":"a17da7d8-7784-4138-8eac-d11cd7a87efc","links":["7940da10-9a09-484f-bc71-ea273ec18e62"],"in":true,"label":"simple","position":"in"}],"model":{"id":"a17da7d8-7784-4138-8eac-d11cd7a87efc","name":"Debug","icon":"M4,5H20V7H4V5M4,9H20V11H4V9M4,13H20V15H4V13M4,17H14V19H4V17Z","color":"#E6EE9C","namespace":"Core.Programming.Debug","version":"0.0.0","abc":[{"name":"Debug","color":"#E6EE9C","delayBefore":0,"delayAfter":0,"continueOnError":false,"version":"0.0.0"},{"optActive":true,"optSysConsole":false,"optDebugData":{"scope":"Message","name":"fruit"}}],"xyz":[{"schema":{"title":"Options","type":"object","properties":{"optActive":{"title":"Active","type":"boolean"},"optSysConsole":{"title":"System Console","type":"boolean"},"optDebugData":{"type":"object","title":"Debug Data","variableType":"String","messageScope":true,"properties":{"scope":{"type":"string"},"name":{"type":"string"}}}}},"uiSchema":{"optDebugData":{"ui:field":"variable"},"ui:order":["optDebugData","optActive","optSysConsole"]},"formData":{"optActive":true,"optSysConsole":false,"optDebugData":{"scope":"Message","name":"fruit"}}}],"outputs":0,"inputs":1}},{"id":"4f523e3c-fb03-484a-855d-9818757a4ce8","type":"simple","selected":true,"x":1113,"y":310,"extras":{"outResult":{"scope":"Message","name":"error"}},"ports":[{"id":"b6fc8ea7-3d77-4119-b6ad-5c5defebced0","type":"default","selected":false,"name":"in","parentNode":"4f523e3c-fb03-484a-855d-9818757a4ce8","links":["5f49c219-a822-49bb-a9ca-187fe7f8ef9d"],"in":true,"label":"simple","position":"in"}],"model":{"id":"4f523e3c-fb03-484a-855d-9818757a4ce8","name":"Stop","icon":"M12,2A10,10 0 0,0 2,12A10,10 0 0,0 12,22A10,10 0 0,0 22,12A10,10 0 0,0 12,2M12,4C16.41,4 20,7.59 20,12C20,16.41 16.41,20 12,20C7.59,20 4,16.41 4,12C4,7.59 7.59,4 12,4M9,9V15H15V9","color":"#fb2727","namespace":"Core.Flow.Stop","version":"0.0.0","abc":[{},{"outResult":{"scope":"Message","name":"error"}}],"xyz":[{"schema":{"title":"Output","type":"object","properties":{"outResult":{"type":"object","title":"Error","variableType":"String","messageOnly":true,"properties":{"scope":{"type":"string"},"name":{"type":"string"}}}}},"uiSchema":{"ui:order":["outResult"],"outResult":{"ui:field":"variable"}},"formData":{"outResult":{"scope":"Message","name":"error"}}}],"outputs":0,"inputs":1}}],"subFlows":[]}

‚Äč