Reduce
This formula allows you to reduce an array (list) of values into a single value. A typical use case for this formula is to sum all the numbers in an array:
{
"type": "reduce",
"components": {
"type": "stat",
"stat": "a_stat_with_a_list_of_numbers"
},
"reducer": {
"type": "add",
"components": {
"type": "list",
"components": [
{
"type": "stat",
"stat": "$reduceAccumulator"
},
{
"type": "stat",
"stat": "$reduceValue"
}
]
}
}
}
JSON Format
{
"type": "reduce",
"components": ...,
"reducer": ...,
"reduce_accumulator_key": ...,
"reduce_value_key": ...
}
Fields
| Field | Type | Required | Description |
|---|---|---|---|
components | StatFormulaComponent | ✓ | The formula for the array (list) of elements you want to reduce. |
reducer | StatFormulaComponent | ✓ | The formula for the reduction to perform on each element of the given components list. It has to be non-empty. Anything inside this formula can make use of the $reduceValue stat, which will contain the current value for the iteration (e.g: if iterating the array ['a', 'b', 'c'], $reduceValue will first be equal to 'a', then 'b' and then 'c'), thus allowing the formula specified here to access the value of each element in the array one at a time as part of the reducer. It can also make use of the $reduceAccumulator value, which will have the result of executing the reducer function up to that value. Note that the first iteration will actually not execute the reducer and instead set the $reduceAccumulator to be equal to the first element. For example, if the reducer function is just concatenating $reduceValue and $reduceAccumulator such that $reduceValue comes first, the iterations will look like this: 1. $reduceValue = 'b', $accumulatorValue = 'a', result --> ba 2. $reduceValue = 'c', $accumulatorValue = 'ba', result --> cba If you were to specify the optional reduce_value_key parameter, you should use the key given there instead of $reduceValue to access these values. The same goes for reduce_accumulator_key and $reduceAccumulator |
reduce_accumulator_key | String | Optional parameter that lets you provide a key to use in the reducer formula to access the array's current value in the iteration. e.g: if you are iterating classes, you might want to assign "$class" to the reduce_accumulator_key, to make your system easier to read. This is also helpful when dealing with nested Reduce formulas, as it will allow inner Reduce formulas to access the values of outer filter formulas. | |
reduce_value_key | String | Optional parameter that lets you provide a key to use in the reducer formula to access the array's current value in the iteration. e.g: if you are iterating classes, you might want to assign "$class" to the reduce_value_key, to make your system easier to read. This is also helpful when dealing with nested Reduce formulas, as it will allow inner Reduce formulas to access the values of outer filter formulas. |