The function to invoke for each item. The function will be passed 3 arguments: the key, the value, the struct. You can alternatively pass a Java BiConsumer which will only receive the first 2 args.
parallel
boolean
false
Specifies whether the items can be executed in parallel
false
maxThreads
integer
false
The maximum number of threads to use when parallel = true
ordered
boolean
false
(BoxLang only) whether parallel operations should execute and maintain order
false
Examples
structEach() with an inline function (closure)
Use a function to write out the keys in a structure to the screen
someStruct = {
A : 1,
B : 2,
C : 3
};
structEach( someStruct, ( Any key, Any value ) => {
writeOutput( "Key " & key & " is " & value & "; " );
} );
Result: Key a is 1; Key b is 2; Key c is 3;
Using a function reference
favs = {
COLOR : "blue",
FOOD : "pizza",
SPORT : "basketball"
};
// named function
// notice that the function takes two arguments, the key and value pair of the current iteration of the structure's key-value pairs
function getFavorites( Any key, Any value ) {
writeOutput( "My favorite " & key & " is " & value );
}
// run structEach() with a named function
structEach( favs, getFavorites );
// run structEach() with an inline function
structEach( favs, ( Any key, Any value ) => {
writeOutput( "My favorite " & key & " is " & value );
} );
Using the member function
statusCodes = {
OK : 200,
CREATED : 201,
NOT_MODIFIED : 304,
BAD_REQUEST : 400,
NOT_FOUND : 404
};
statusCodes.each( ( Any key, Any value ) => {
writeOutput( "#key# => #value#<br />" );
} );
Result: NOT_FOUND => 404
BAD_REQUEST => 400
CREATED => 201
OK => 200
NOT_MODIFIED => 304
Accessing a reference to the looping struct in the callback
statusCodes = {
OK : 200,
CREATED : 201,
NOT_MODIFIED : 304,
BAD_REQUEST : 400,
NOT_FOUND : 404
};
statusCodes.each( ( Any key, Any value, Any struct ) => {
writeDump( struct );
} );