Maps

Iteration that transforms each item of an array into a value.

Maps transform each item of an array into a value (typically an element or elements). The template reads the current item and index through the $item and $index scoped arguments:

{
  "$map": ["foo", "bar", "baz"],
  "$each": {
    "type": "text",
    "props": { "children": { "$arg": "$item" } }
  }
}

Required

  • $map (expression): The array to map over: an array literal, a data reference, or a scoped argument.
  • $each (template): One item of the position's array. In children positions the template is one child, so nested conditionals and maps compose freely. The resolved array MUST still satisfy the prop's schema.

Behavior

The map resolves to an array holding one resolved template per source item. Maps are therefore valid in positions that accept an array: a children prop, or a prop (a bindable field included) whose declared schema accepts an array directly, through a multi-valued type, or through a composition or conditional branch. The template is validated as one item of that array where the meta-schema can identify a single item schema structurally; where it cannot (tuples, untyped arrays, several array branches), the meta-schema accepts any template, and satisfying the prop's schema with the resolved array remains the document's obligation. A source that does not resolve to an array resolves the map to an empty array.

Maps may nest. Within a nested template, $item and $index refer to the innermost map's iteration; the nested map's $map source is still resolved in the enclosing scope.

Example

Transform data into elements:

{
  "type": "list",
  "props": {
    "children": {
      "$map": { "$data": "/names" },
      "$each": {
        "type": "list-item",
        "props": { "children": { "$arg": "$item" } }
      }
    }
  }
}

On this page