Description

This function loops over elements in a structure by accessing key-value pairs.

Returns

Nothing

Category

Syntax

StructEach(struct,function(key, value) {});

Parameters

Parameter

Description

struct

Name of the structure object.

function

Inline function executed for each key - value pair in the struct.

key

Key in a struct.

value

Value in a struct.

Example using an inline function

<cfscript>
       myStruct=StructNew();
       myStruct = {a:1,b=2,c=3,d=4,e=5}; // Define key-value pairs
       // Run StructEach with an inline function
       StructEach(myStruct,function(key,value) {
       WriteOutput('The value of ' & key & ' is ' & value & '; ');
});
</cfscript>

Output

The value of a is 1; The value of b is 2; The value of c is 3; The value of d is 4; The value of e is 5;

Example using a named function

<cfscript>
       myStruct=StructNew();
       myStruct = {team:"Chicago Bulls",player:"Michael Jordan", sport:"basketball"}; // Define key-value pairs
       // Create a named function that takes two arguments as key and value
      function getValues(key, value) { 
      writeOutput('My favorite ' & key & ' is ' & value & " | "); 
      } 
      // Run StructEach with the named function getValues
      structEach(myStruct,getValues); 
</cfscript>

Output

My favorite player is Michael Jordan | My favorite sport is basketball | My favorite team is Chicago Bulls |

Using member function

<cfscript>
       myStruct = {a:1,b=2,c=3,d=4,e=5}; // Define key-value pairs
       myStruct.each(function(key,value){
             WriteOutput("#key#:#value#");
       });
</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