The collection.reduce( ) method applies the methodName callback method against an accumulator and each element in the collection (from left to right) to reduce it to a single value.
Note: This method does not modify the original collection.
In methodName, pass the name of the method to use to evaluate collection elements, along with its parameter(s) in param (optional). methodName takes each collection element and performs any desired operation to accumulate the result into $1.accumulator, which is returned in $1.value.
You can pass the value to initialize the accumulator in initValue. If omitted, $1.accumulator starts with Undefined.
methodName receives the following parameters:
- in $1.value: element value to be processed
- in $2: param
- in $N...: param2...paramN
methodName sets the following parameter(s):
- $1.accumulator: value to be modified by the function and which is initialized by initValue.
- $1.stop (boolean, optional): true to stop the method callback. The returned value is the last calculated.
With the following Multiply method:
This example allows reducing several collection elements to a single one:
With the following Flatten method:
If($1.accumulator=Null)
$1.accumulator:=New collection
End if
$1.accumulator.combine($1.value)