For Each
Controlled node
Overview
The For Each node iterates over each item in an input list or object, firing the loop event for every item. During each iteration, the current element and its key (index or property name) are available as data outputs. Once all items have been processed, the finished event fires. This node is essential for processing collections of data one item at a time.
The node automatically handles both arrays (lists) and plain objects (dictionaries). For arrays, the key output will be the numeric index (0, 1, 2...). For objects, the key output will be the string property name and the element will be the corresponding value.
Inputs
| Input | Type | Description | Default |
|---|---|---|---|
| Run | Event | Triggers the node to start iterating over the input object. | - |
| Object | Data | The list or object to iterate over. Accepts arrays or plain objects. | - |
Outputs
| Output | Type | Description |
|---|---|---|
| Loop | Event | Fires for each item in the collection. Connect subsequent processing nodes here. |
| Element | Data | Contains the current item being iterated (array element or object value). |
| Key | Data | Contains the index (for arrays) or key (for objects) of the current item. |
| Finished | Event | Fires once when all items have been processed and iteration is complete. |
Runtime Behavior and Defaults
When the Run event fires, the node begins iterating over the input Object. For each item, it sets the Element and Key outputs, then fires the Loop event. The node then repeats this process automatically for the next item until the end of the collection is reached.
- Arrays: The node iterates from index
0tolength - 1. The Key output will be the integer index. - Objects: The node iterates over all enumerable properties. The Key output will be the string property name.
- Empty collections: If the input is an empty array or object, the Loop event never fires and the Finished event fires immediately.
- Non-collection inputs: If the input is neither an array nor an object, the node behavior is undefined.
Example Usage
Processing a List of Records
- Connect a list of records (e.g., from a database query) to the Object input.
- Connect the Run event to trigger the iteration.
- Connect the Loop event to an AI Write node that processes each record.
- Use the Element output to pass the current record to the AI Write node.
- Connect the Finished event to a node that runs after all records have been processed (e.g., sending a completion notification).
Iterating Over Object Properties
- Connect a dictionary/object to the Object input.
- Connect the Loop event to a node that uses the Key output to log property names and the Element output to access values.
- The Key output will contain strings like
"name","email", etc., while Element contains the corresponding values.