For the complete documentation index, see llms.txt. This page is also available as Markdown.

reduce

The reduce function folds an array into a single value using an accumulator. It works in a similar way to JavaScript's Array.prototype.reduce.

Syntax

<array>.reduce(expr, init, update)

Arguments

  • expr - An expression evaluated against each element to produce the current value. Use $this to refer to the current element, or a field name to extract a property.

  • init - The initial value of the accumulator.

  • update - An expression evaluated for each element to produce the next accumulator value. Use $acc to refer to the current accumulator, $this to refer to the value produced by expr, and $key to refer to the current index.

Examples

Sum numbers

[1, 2, 3, 4].reduce($this, 0, $acc + $this)
// 10

Concatenate strings

["a", "b", "c"].reduce($this, "", $acc + $this)
// "abc"

Product of numbers

[2, 3, 4].reduce($this, 1, $acc * $this)
// 24

Sum a field from objects

CLI usage

Sum of indices using $key

Chained with other expressions

Last updated