$$reduce
Reduce an array with an initial value (identity
) and a context transformer to a single value
Usage​
{
"$$reduce": /* Array of elements */,
"to": /* Transformer(##accumulator,##current,##index) */,
"identity": /* any */
}
"$$reduce(<to>,<identity>):{input}"
Returns​
The reduced value (type of to
or identity
)
Arguments​
Argument | Type | Values | Required / Default Value | Description |
---|---|---|---|---|
Primary | array | Yes | Array of elements | |
to | Transformer(##accumulator ,##current ,##index ) | Yes | Transformer to apply on each element (with last accumulation) to get the next accumulation
| |
identity | any | Yes | Initial value to start the accumulation with |
Examples​
Input
Definition
Output
{
"items": [
{
"id": "aaa",
"amount": 10
},
{
"id": "bbb",
"amount": 20
}
]
}
{
"$$reduce": "$.items",
"identity": 0,
"to": {
"$$math": [
"##accumulator",
"+",
"##current.amount"
]
}
}
30
{
"items": [
{
"id": "aaa",
"amount": 10
},
{
"id": "bbb",
"amount": 20
}
]
}
{
"$$reduce": "aaa",
"identity": "bbb",
"to": "##current"
}
null
{
"items": [
{
"id": "aaa",
"amount": 10
},
{
"id": "bbb",
"amount": 20
}
]
}
"$$reduce('$$math(##accumulator,+,##current.amount)',0):$.items"
30