Skip to main content

$$reduce

Reduce an array with an initial value (identity) and a context transformer to a single value.

Usage​

{
"$$reduce": [ /* values */ ],
"identity": /* initial value */,
"to": /* Transformer */
}

Returns​

Type of to's result or identity's

Arguments​

ArgumentTypeValuesRequired / Default ValueDescription
PrimaryarrayYesArray of elements
toTransformer(##accumulator,##current,##index)Input is ##accumulator (previous accumulator value), ##current (current element) & ##index (current index)YesTransformer to apply on each element (with last accumulation) to get the next accumulation
identityYesInitial value to start the accumulation with

Examples​

Input

Definition

Output

[
{
"amount": 10
},
{
"amount": 20
}
]
{ 
"$$reduce":"$",
"to": "$$math(##accumulator,+,##current.amount)",
"identity": 1
}
31