ListReduce

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, callback, initialValue, [delimiter, includeEmptyFields])

History

ColdFusion (2018 release): Introduced named parameters.

ColdFusion 11: Added this function.

Parameters

Parameter

Req/Opt

Default

Description

list Required   The input list.
callback Required  

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

  • result : This function reduces the list to a single value and returns the value. The value is stored in result.
  • 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.
delimiter Optional comma (,) The list delimiter. The type is string .
includeEmptyFields Optional false Include empty values. The type is boolean.

Example

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

Output

57

 Adobe

Get help faster and easier

New user?

Adobe MAX 2024

Adobe MAX

The Creativity Conference

Oct 14–16 Miami Beach and online

Adobe MAX 2024

Adobe MAX

The Creativity Conference

Oct 14–16 Miami Beach and online