arrayReduce
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.
arrayReduce(array, function(result, item [,index, array]){} [, initialValue]) → returns any
Member Function Syntax
someArray.reduce(function(result, item [,index, array]){} [, initialValue])
Argument Reference
array array Required
the input array
callback any Required
Closure or a function reference that will be called for each of the iteration.
Callback parameters:
-
result* any : The result of the reduce operation after the previous iteration -
value* any : The value for the current iteration -
index* numeric : The current index for the iteration -
array* array : A reference of the original array
initialValue any
Initial value which will be used for the reduce operation.
Links more information about arrayReduce
- Practical uses for reduce functions - Blog entry by John Whish covering some practical uses for reduce functions in CFML.
- Map, Reduce and other Higher Order Functions - A Primer for map, reduce, filter, and Higher Order Functions in general.
- ColdFusion 11: .map() and .reduce() - A look at the new .map() and .reduce() methods that each of array, struct and lists all have
Examples Sample code invoking the arrayReduce function
Sum each a element in the array
complexData = [ {a: 4}, {a: 18}, {a: 51} ];sum = arrayReduce( complexData, function(prev, element){
return prev + element.a;
}, 0 );
writeOutput(sum); Expected Result: 73
© 2012–present Foundeo, Inc. and the CFDocs contributors.
Licensed under the MIT License.
ColdFusion is a trademark of Adobe Systems Incorporated.
https://cfdocs.org/arrayreduce