FOR EACH Loops¶
FOR EACH iterates over array-like values and executes a block for every item.
Basic loop¶
The loop reads input.items and outputs each element's name field.
Filtering items¶
An optional WHERE clause keeps only matching elements.
Only items with quantity greater than 1 reach the body.
Mutating within a loop¶
Use SET to update accumulators as you iterate:
LET totals = { count: 0, sum: 0 };
FOR EACH item IN input.items WHERE item.qty > 0 {
SET totals.count = totals.count + 1;
SET totals.sum = totals.sum + item.qty;
}
OUTPUT totals;
Tips¶
FORis a synonym ofFOR EACH.WHEREis optional; omit it when you don’t need filtering.- Use
LETinside the loop for per-item scratch variables; useSETto mutate values defined outside the loop. - For single-expression transformations, consider array comprehensions instead.
Try it¶
Load the collection-transforms example in the playground and compare the loop to MAP/FILTER/REDUCE alternatives.