ForEach Component
Use the ForEach component to loop through an array of items. It allows you to run a set of actions for each item in the list, making it perfect for processing batch data, transforming lists, or running sequential tasks.
What You’ll Configure
- Provide an Input Array
- Build the Loop Logic
- Select an Output Format
- Handle the Aggregated Results
- Best Practices
- Troubleshooting Tips
- What to Try Next
Step 1: Provide an Input Array
The ForEach
component requires a single input: the array you want to loop through.
Input | Required? | Description | Notes |
---|---|---|---|
Array | Yes | The list of items to be processed one by one. | The input must be a valid JSON array. e.g., [1, 2, 3] or ["a", "b"] . |
Step 2: Build the Loop Logic
Inside the ForEach
component, you'll define the sequence of actions to be performed for each item in the array. Two special variables are available within this loop:
Variable | Description |
---|---|
item | The value of the current item being processed in the loop. |
index | The numerical position (starting from 0) of the current item. |
You can drag any component into the loop branch and use {{item}}
and {{index}}
as dynamic inputs.
Step 3: Select an Output Format
This setting controls the structure of the final aggregated Result
output after the loop completes.
Full: Returns a detailed object for each iteration, including the item, index, and the full output of the last component inside the loop.
Use this for debugging or when you need comprehensive data from each step.
Step 4: Handle the Aggregated Results
After the loop has finished processing all items, the ForEach
component provides a single output containing the aggregated results from all iterations.
Output | Description | Data Structure |
---|---|---|
Result | An array containing the collected outcomes from all iterations. | The structure depends on the selected Output Format. |
// Example with Output Format: "Full"
[
{ "index": 0, "item": "apple", "result": { "output": "processed_apple" } },
{ "index": 1, "item": "banana", "result": { "output": "processed_banana" } }
]
// Example with Output Format: "Array of Results"
[
"processed_apple",
"processed_banana"
]
Best Practices
- Keep Loop Logic Simple: For complex operations, have the loop call an Agent Skill. This keeps your main workflow clean and makes the logic reusable.
- Handle Empty Arrays: Add a Condition block before the
ForEach
to check if the input array is empty to avoid running the loop unnecessarily. - Mind Your API Limits: If you are making an API call inside the loop, be aware of rate limits. A long list can quickly exhaust your quota. Consider adding a delay if needed.
- Use "Array of Results" for Transformations: When your goal is to map one array to another (e.g., extracting emails from a user list), this format is the most efficient.
Troubleshooting Tips
What to Try Next
- Combine
ForEach
with an API Call to enrich each item in a list with data from an external service. - Feed the
Result
of aForEach
loop into a GenAI LLM Component to generate a summary of the processed items.