Used to loop over elements in a structure by accessing key-value pairs.
structEach(struct,function(key, value [, struct]){} [, parallel] [, maxThreads])
→ returns void
someStruct.each(function(key, value [, struct]){} [, parallel] [, maxThreads])
key
*
string
:
The key for the current iteration
value
*
any
:
The value for the current iteration
struct
*
struct
:
A reference of the original struct
false
true
false
20
Use a function to write out the keys in a structure to the screen
someStruct = {a=1,b=2,c=3};
structEach(someStruct,function(key,value) {
writeOutput('Key ' & key & ' is ' & value & '; ');
});
Expected Result: Key a is 1; Key b is 2; Key c is 3;
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(key, value) {
writeOutput('My favorite ' & key & ' is ' & value);
}
// run structEach() with a named function
structEach(favs,getFavorites);
// run structEach() with an inline function
structEach(favs, function(key,value) {
writeOutput('My favorite ' & key & ' is ' & value);
});
CF 11+ Lucee 4.5+
statusCodes = {
OK = 200,
CREATED = 201,
NOT_MODIFIED = 304,
BAD_REQUEST = 400,
NOT_FOUND = 404
};
statusCodes.each(function(key, value) {
writeOutput("#key# => #value#<br />");
});
Expected Result: NOT_FOUND => 404 BAD_REQUEST => 400 CREATED => 201 OK => 200 NOT_MODIFIED => 304
Lucee 4.5+
statusCodes = {
OK = 200,
CREATED = 201,
NOT_MODIFIED = 304,
BAD_REQUEST = 400,
NOT_FOUND = 404
};
statusCodes.each(function(key, value, struct) {
writeDump(struct);
});
Signup for cfbreak
to stay updated on the latest news from the ColdFusion / CFML community. One email, every friday.