For Each Loop
Learn how to use For Each loop
When you need to iterate over an array, you can use the "For Each" loop under the Programming section.
For Each node has one input port and two output ports. The 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 For Each node's properties as below. The current item property will contain the iterated value.
When you finished processing the item through the top 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 copy the below code and click an empty region of your board then press Ctrl+v.
{"nodes":[{"id":"0533a62b-2dd4-42b5-a095-84bdeb58da3b","type":"simple","selected":true,"x":480,"y":390,"extras":{"inPayload":{"scope":"JS","name":"Date.now()"},"outPayload":{"scope":"Message","name":"payload"},"optRepeat":1,"optOnce":true,"optOnceDelay":0.1},"ports":[{"id":"5c3d76d2-1de3-4a4d-92fc-0344c87f07f8","type":"default","selected":false,"name":"out_5c3d76d2-1de3-4a4d-92fc-0344c87f07f8","parentNode":"0533a62b-2dd4-42b5-a095-84bdeb58da3b","links":["9bf51ff1-6aff-41a8-a024-cb201d3dbccc"],"in":false,"label":"simple","position":"out"}],"model":{"id":"0533a62b-2dd4-42b5-a095-84bdeb58da3b","name":"Start","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":[{"name":"Start","color":"#FDD835","version":"0.0.0"},{"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"],"optRepeat":{"ui:field":"input"},"optOnce":{"ui:field":"input"},"optOnceDelay":{"ui:field":"input"}},"formData":{"optRepeat":1,"optOnce":true,"optOnceDelay":0.1}}],"outputs":1,"inputs":0}},{"id":"298aaf7e-4dc5-49f3-a9aa-a7c049f60093","type":"simple","selected":true,"x":750,"y":390,"extras":{"outputs":1,"func":"msg.fruits=[\"Apple\", \"Orange\", \"Banana\"];\nreturn msg;\n","optTimeout":30,"variables":[]},"ports":[{"id":"b5806375-74ad-41fc-b682-8c33bceab9d1","type":"default","selected":false,"name":"out_b5806375-74ad-41fc-b682-8c33bceab9d1","parentNode":"298aaf7e-4dc5-49f3-a9aa-a7c049f60093","links":["4d1d03c6-6a44-4b60-97a6-e90d3ac02973"],"in":false,"label":"simple","position":"out"},{"id":"7dc5839a-3b96-48c8-add3-8f26938d55a4","type":"default","selected":false,"name":"in","parentNode":"298aaf7e-4dc5-49f3-a9aa-a7c049f60093","links":["9bf51ff1-6aff-41a8-a024-cb201d3dbccc"],"in":true,"label":"simple","position":"in"}],"model":{"id":"298aaf7e-4dc5-49f3-a9aa-a7c049f60093","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","version":"0.0.0","color":"#F27925","delayBefore":0,"delayAfter":0,"continueOnError":false},{"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:field":"input"},"func":{"ui:widget":"hidden","ui:options":{"rows":6}},"optTimeout":{"ui:field":"input"}},"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":"60004d2b-1aaa-4d31-bf7d-7a25174b1335","type":"simple","selected":true,"x":1060,"y":380,"extras":{"optInput":{"scope":"Message","name":"fruits"},"optOutput":{"scope":"Message","name":"fruit"}},"ports":[{"id":"9852f11e-5025-4768-bd21-b8639453eef9","type":"default","selected":false,"name":"out_9852f11e-5025-4768-bd21-b8639453eef9","parentNode":"60004d2b-1aaa-4d31-bf7d-7a25174b1335","links":["14b402d0-414a-4bfd-b4b8-c8c87de7f1a6","d56adbe5-1f8b-4238-9277-d6b17a6802c1"],"in":false,"label":"simple","position":"out"},{"id":"98c93474-7ebc-4637-bca8-a1685c3efa9a","type":"default","selected":false,"name":"out_98c93474-7ebc-4637-bca8-a1685c3efa9a","parentNode":"60004d2b-1aaa-4d31-bf7d-7a25174b1335","links":["65857ea0-aba8-41ec-873c-d372999d9bc8"],"in":false,"label":"simple","position":"out"},{"id":"aedd6019-0c81-4984-8821-dcaba98fb2b1","type":"default","selected":false,"name":"in","parentNode":"60004d2b-1aaa-4d31-bf7d-7a25174b1335","links":["4d1d03c6-6a44-4b60-97a6-e90d3ac02973","5bb5918d-862c-41ee-90e2-ca773ca10488"],"in":true,"label":"simple","position":"in"}],"model":{"id":"60004d2b-1aaa-4d31-bf7d-7a25174b1335","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":"c3fcef50-986b-4750-86c9-779415ecdcf6","type":"simple","selected":true,"x":1360,"y":380,"extras":{"optNodes":{"all":false,"ids":["05999ba5-92bb-4af1-af28-cdbe38f93636"]}},"ports":[{"id":"5e27c79c-0244-44e1-bf56-e75b17e0d8ce","type":"default","selected":false,"name":"in","parentNode":"c3fcef50-986b-4750-86c9-779415ecdcf6","links":["d56adbe5-1f8b-4238-9277-d6b17a6802c1"],"in":true,"label":"simple","position":"in"}],"model":{"id":"c3fcef50-986b-4750-86c9-779415ecdcf6","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","version":"0.0.0","color":"#444","delayBefore":0,"delayAfter":0,"continueOnError":false},{"optNodes":{"all":false,"ids":["05999ba5-92bb-4af1-af28-cdbe38f93636"]}}],"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":["05999ba5-92bb-4af1-af28-cdbe38f93636"]}}}],"outputs":0,"inputs":1}},{"id":"05999ba5-92bb-4af1-af28-cdbe38f93636","type":"simple","selected":true,"x":800,"y":490,"extras":{},"ports":[{"id":"c6f71d73-bc6b-48f5-86cc-dc6b3c26bbc0","type":"default","selected":false,"name":"out_c6f71d73-bc6b-48f5-86cc-dc6b3c26bbc0","parentNode":"05999ba5-92bb-4af1-af28-cdbe38f93636","links":["5bb5918d-862c-41ee-90e2-ca773ca10488"],"in":false,"label":"simple","position":"out"}],"model":{"id":"05999ba5-92bb-4af1-af28-cdbe38f93636","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":"2f785ec1-2696-439d-8369-67c30c71546d","type":"simple","selected":true,"x":1360,"y":300,"extras":{"optActive":true,"optSysConsole":false,"optDebugData":{"scope":"Message","name":"fruit"}},"ports":[{"id":"9706fe39-d99c-47bf-828c-5f5a5c717431","type":"default","selected":false,"name":"in","parentNode":"2f785ec1-2696-439d-8369-67c30c71546d","links":["14b402d0-414a-4bfd-b4b8-c8c87de7f1a6"],"in":true,"label":"simple","position":"in"}],"model":{"id":"2f785ec1-2696-439d-8369-67c30c71546d","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":"31d85794-2c70-4e6b-a7ce-4dc31036a66b","type":"simple","selected":true,"x":1360,"y":490,"extras":{"outResult":{"scope":"Message","name":"error"}},"ports":[{"id":"89b2b423-f9f1-434c-bde9-05d9bd925db8","type":"default","selected":false,"name":"in","parentNode":"31d85794-2c70-4e6b-a7ce-4dc31036a66b","links":["65857ea0-aba8-41ec-873c-d372999d9bc8"],"in":true,"label":"simple","position":"in"}],"model":{"id":"31d85794-2c70-4e6b-a7ce-4dc31036a66b","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":[]}