StructEach()
Applies the function to each of the elements in the structure
Usage
BOOLEAN = StructEach(
struct,
function
)
Argument | Summary |
---|---|
struct | Structure Object |
function | function to loop over the data, passing in the key and element; function(key, element){} |
Calling
Supports named-parameter calling allowing you to use the function like:
StructEach( struct=?, function=? );
Supports passing parameters as a structure using ArgumentCollection:
StructEach( ArgumentCollection={ struct : ?, function : ? } );
Extra
You can easily iterate over the elements of a structure by using the StructEach() function or the .each() feature.
<cfscript> str = { name : "alan", age : 21, dob : now() }; // Callback function declared that accepts the element function mapCallback(k,v){ Console( k & "=" & v ); } // Loop around each element StructEach( str, mapCallback ); // Alternatively you can loop using the .each() str.each( mapCallback ); </cfscript>
For each element in the array, the function 'mapCallback' is called, passing in the current element and the key. The callback function sits within the variable scope of the calling function, so it can interact with variables outside of itself.