Last updated on
Jan 11, 2022
|
Also applies to ColdFusion
Description
Iterates over every entry of the array and calls the closure to work on the elements of the array. This function will reduce the array to a single value and will return the value.
Returns
Any
Syntax
ArrayReduce(array, callback,[ initialValue=null]) |
History
ColdFusion (2018 release): Changed parameter function to callback.
ColdFusion 11: Added this function.
Parameters
Parameter |
Req/Opt |
Default |
Description |
---|---|---|---|
array | Required | The input array. | |
callback | Required | Closure or a function reference that will be called for each of the iteration . The arguments passed to the callback are
|
|
initialValue | Optional | Initial value which will be used for the reduce operation. The type is any. |
Example
<cfscript> arr = [1,2,3,4,5]; function square(element, index) { writeOutput("index is " & index); return element * element; } sq = arrayMap(arr, square); writeDump(sq); result = arrayReduce(sq, function(value, element) { value = value?:0; value += element; return value; }); writeDump(result); </cfscript> |