for

Runs a block of commands once for each element of an iterable: a list, a dict, or any Python iterable or async iterable that an expression produces.

Fields

FieldTypeDefaultDescription
iterrequireddynamic string or list or objectWhat to iterate over: a list, dict or any Python iterable.
loopVarlist of name or nameNames bound on each iteration. For a tuple of N elements, give N names.
docommand blockCommands executed for each element produced by the iterator.
maxIterationsdynamic string or integerCaps the number of iterations.

Example

"for": {
  "iter": "{ result['properties']['periods'][:8] }",
  "loopVar": "period",
  "do": {
    "var": {
      "lines": "{ lines + [f'In {period['number']} hours: {period['temperature']} and {period['shortForecast']}'] }"
    }
  }
}

Iterating a dict binds the dict's keys. To get key and value together, iterate { d.items() } and give two loop variables:

"for": {
  "iter": "{ prices.items() }",
  "loopVar": ["sku", "price"],
  "do": {
    "progress": "{ f'{sku} costs {price}' }"
  }
}

Rules

  • iter may also be a literal JSON list or object. An expression inside such a literal is evaluated like any other.
  • When an element is a tuple and several loop variables are given, the tuple is unpacked into the variables in order. A single loop variable over pairs, such as { d.items() }, gets the whole pair.
  • The loop variables are locals of the function, removed when the loop ends. They follow the rules for variable names: no reserved name, no Python keyword.
  • A for nested inside another for may not reuse the outer loop's variable names.
  • maxIterations stops the loop quietly when the count is reached, and 0 means zero iterations. Without it, a loop that reaches the max loop iterations setting in Runtime limits fails the run with a message, as while does.
  • ask is not allowed inside a for block, nor in any function called from one.