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
$thisto 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
$accto refer to the current accumulator,$thisto refer to the value produced byexpr, and$keyto refer to the current index.
Examples
Sum numbers
[1, 2, 3, 4].reduce($this, 0, $acc + $this)
// 10Concatenate strings
["a", "b", "c"].reduce($this, "", $acc + $this)
// "abc"Product of numbers
[2, 3, 4].reduce($this, 1, $acc * $this)
// 24Sum a field from objects
CLI usage
Sum of indices using $key
Chained with other expressions
Last updated