Zebflow Docs
logic.reduce
Zebflow Docs

logic.reduce

Folds the per-item answers of a logic.foreach into one value

logic.reduce --initial EXPR --step EXPR → reduce

Folds the per-item answers of a logic.foreach into one value. It closes the loop: placed after logic.foreach (directly or further down the item branch), it runs once, after every item has run — --initial once, then --step for every item that reached it, in item order, with $acc the accumulator so far and $input that item's payload (an item whose branch was skipped is left out). An empty list, or a series where every item was skipped, answers --initial. Expressions are JavaScript values without {{ }}. Answers one key, reduce: the final $acc (input.reduce.total), on top of the payload the foreach received; the loop's own nodes are not in $nodes after it.

Flags

  • --initial (required): The initial accumulator: a JavaScript value ({ total: 0 }).
  • --step (required): The next accumulator from $acc and $input: a JavaScript value.

Examples

Sum a column

| logic.reduce --initial "{ total: 0, n: 0 }" --step "{ total: $acc.total + $input.item.amount, n: $acc.n + 1 }"

After logic.foreach --from "input.query.rows" over three rows: the payload the loop started from, with the sum under reduce.

It answers:

json
{  "query": {    "rows": [      {        "amount": 10      },      {        "amount": 20      },      {        "amount": 12.5      }    ]  },  "reduce": {    "total": 42.5,    "n": 3  }}