$$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​
Argument | Type | Values | Required / Default Value | Description |
---|---|---|---|---|
Primary | array | Yes | Array of elements | |
to | Transformer(##accumulator ,##current ,##index ) | Input is ##accumulator (previous accumulator value), ##current (current element) & ##index (current index) | Yes | Transformer to apply on each element (with last accumulation) to get the next accumulation |
identity | Yes | Initial value to start the accumulation with |
Examples​
Input
Definition
Output
[
{
"amount": 10
},
{
"amount": 20
}
]
{
"$$reduce":"$",
"to": "$$math(##accumulator,+,##current.amount)",
"identity": 1
}
31