Whatever message this page gives is out now! Go check it out!

StructReduce

Last update:
May 18, 2026

Description

Iterates over every entry of the struct and calls the closure to work on the key value pair of the struct.

Returns

Any

Syntax

StructReduce(struct, function(result, key, value [,struct]), initialVal)

History

ColdFusion 11: Added this function.

Parameters

Parameter
Req/Opt
Default
Description
structRequired The input struct.
functionRequired 
Closure or a function reference that will be called for each of the iteration. The arguments passed to the callback are
  • result: the result of the reduce operation after the previous iteration
  • key: key for the current iteration
  • value: the value for the current iteration
  • struct: a reference of the original struct
initialValOptional Initial value that will be used for the reduce operation. The type is any.

Example

<cfscript>
       person=StructNew();
       person = {fname="John", lname="Doe"};

       ucaseStruct = structMap(person, function(key, value)
       {
             return ucase(value);
       });
       writeDump(ucaseStruct);

       concatValues = structReduce(person, function(result, key, value)
       {
             result = result?:"";
             result&= value & " ";
             return result;
       });

       writeDump(concatValues);
</cfscript>

Output

Using member function

<cfscript>
       myStruct=StructNew();
       myStruct={a=1,b=2,c=3,d=4,e=5};
       myReduce=myStruct.reduce(function(result,key,value){
             return result+value;
       },10);
       WriteDump(myReduce); // Returns 25
</cfscript>

Share this page

Was this page helpful?
We're glad. Tell us how this page helped.
We're sorry. Can you tell us what didn't work for you?
Thank you for your feedback. Your response will help improve this page.

On this page