Skip to content

Array Comprehensions

Array comprehensions build a new array from an existing collection in a single expression.

When to use it

Use a comprehension when you only need a simple mapping or filtering expression and want a concise result.

Syntax

LET squares = [n * n FOR EACH n IN input.numbers]
LET evens = [n FOR EACH n IN input.numbers WHERE n % 2 == 0]

Example

TRANSFORM Tags {
    LET tags = [item.tag FOR EACH item IN input.items]
    OUTPUT { tags: tags }
}

Pitfalls

  • Comprehensions allow only a single result expression before FOR EACH.
  • Use FOR EACH if you need multiple statements or side effects.

Try it

Open collection-transforms to see comprehensions alongside MAP/FILTER/REDUCE.