Description

Iterates over each item of the list and calls the closure to work on the item. This function will reduce the list to a single value and will return the value.

Returns

Any

Syntax

ListReduce(list, function(result, item [,index, list]) [,initialValue, delimiter, includeEmptyFields])

History

ColdFusion 11: Added this function.

Parameters

Parameter

Req/Opt

Default

Description

list Required   The input list.
function Required  

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

  • item: value
  • Index: current index for the iteration
  • list: reference of the original list
initialValue Optional   Initial value which will be used for the reduce operation. The type is any.
delimter Optional comma (,) The list delimiter. The type is string.
includeEmptyFields Optional false Include empty values. The type is boolean.

Example

<cfscript>


list = "red, blue, green, yellow";
ucaseList = listMap(list, function(item)
{
return ucase(item);
});


writeDump(ucaseList);
writeOutput("<br>");


concatValues = listReduce(list, function(result, item, index, mylist, delimiter)
{
result = result?:"";
if(index == 1)
delimiter = "";
result&= delimiter & item;
return result;
});
writeDump(concatValues);

</cfscript>
<cfscript>
       myList="23,54,87,98,11,35,91";
       MyVal=ListReduce(myList,function(value1,value2){
             return (value1+value2/ListLen(myList)); // Calculates the average of the values in the list
       },0); // Initial value is 0
       WriteOutput(#myVal#);
</cfscript>

Output

57

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