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

struct Required   The input struct.
function Required  

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
initialVal Optional   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

StructReduce

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>

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