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, function(result, item, [,index, array])[, initialValue])

History

ColdFusion 11: Added this function.

Attributes

Attribute

Req/Opt

Default

Description

array Required   The input array.
function Required  

Closure or a function reference that will be called for each of the iteration. The arguments passed to the callback are

  • result: result of the reduce operation after the previous iteration
  • item: item in the array
  • index : current index for the iteration
  • array : reference of the original array
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>

This work is licensed under a Creative Commons Attribution-Noncommercial-Share Alike 3.0 Unported License  Twitter™ and Facebook posts are not covered under the terms of Creative Commons.

Legal Notices   |   Online Privacy Policy