# logic.foreach

Runs the nodes wired to its item pin once per element of an array

# logic.foreach

```
logic.foreach --from EXPR [--dispatch sequential] [--batch-size N] [--keep-input]
```

Runs the nodes wired to its `item` pin once per element of an array. Evaluates `--from` (JavaScript over `input`) to an array and runs everything wired to its `item` pin once per element, in order. Each run receives `{ item, index, count }` — the element is `input.item`, not `input`; the upstream payload is not carried unless `--keep-input` (then it is merged in beside `item`). `$item`, `$index` and `$count` name the run's element anywhere down the branch, even after a node replaced the payload. Each element runs on its own — its own skips and `$nodes` — and finishes before the next starts. The body ends at the [`logic.reduce`](/nodes/logic/logic.reduce.md) (fold) or [`logic.collect`](/nodes/logic/logic.collect.md) (list) that closes it, which runs once after the last element, on the payload this node received; an empty list closes too. After the close the body's nodes are not in `$nodes`, and an edge from the body to anywhere but its close is refused. `--batch-size N` emits arrays of up to N elements instead of single elements. A non-array expression fails the node.

## Flags

- `--from` (required): The list: JavaScript over `input` (and `$trigger`, `$nodes`) returning the array to emit.
- `--dispatch`: How the emissions run: `sequential` (the default, and the only policy today).
- `--batch-size`: Emit arrays of up to N elements instead of single elements (at least 1).
- `--keep-input`: Include the full upstream payload in every emitted item. Off by default to avoid fan-out amplification.

## Examples

### One run per row

```
| logic.foreach --from "input.query.rows"
```

The first of two emissions on the `item` pin; the next node reads `input.item.email`.

Input:

```json
{
  "query": {
    "rows": [
      {
        "email": "a@example.com"
      },
      {
        "email": "b@example.com"
      }
    ]
  }
}
```

It answers:

```json
{
  "item": {
    "email": "a@example.com"
  },
  "index": 0,
  "count": 2
}
```

## Used in

- [Agentic Scheduling (AI + Cron)](/examples/agentic-scheduling.md)
- [Web Scraping + Data Pipeline](/examples/scraping.md)
