Earlier today, I was looking through the “New Functions in ColdFusion 10” doc, and noticed a few gems: ArrayEach() and StructEach(). These are huge, because until now, there has been something of a disconnect between dealing with arrays and structures in CF, and their counterparts in other languages, like JavaScript. All JS libraries (like ExtJS) have “each” methods attached to basic objects which allows you to treat collections of the same kind of thing in a bulk way. Fortunately, CF10 introduces two very handy new methods to apply an “each”-like process to both arrays and structures.

NOTE: The ArrayEach() and StructEach() functions act on the original array/struct. Therefore, if you make changes to the array/struct within these functions (as in the ArrayEach() example below), the original array/struct will be affected (thanks Andy!).


So, for example, let’s say you have an array of friends’ names, and you want to make the list all uppercase. Pre-CF10, you might approach this by looping over the array, and executing a function at each iteration. Fine, easy enough, and it works. However, with ArrayEach(), you can ditch the clunkiness of the for-loop, and keep everything self-contained to the array. And, you can execute either an “inline” function, or call a named function. The two examples (which produce the same results) are below:

names = ['John','mary','JOseph','Glenda','Mike'];

// named function
// notice that the function takes one argument, the value of the current iteration of the array
function makeLowercase(obj) {
    names[arrayfind(names,obj)] = lcase(obj);

//  run arrayeach() with an inline function
arrayeach(names,function(obj) {
    names[arrayfind(names,obj)] = lcase(obj);

// run arrayeach() with a named function

See the live example or grab the code from GitHub


Not to be outdone, structures get some each() lovin’ in CF10 as well. But instead of iterating over an array, the StructEach() function executes for every key-value pair in the structure. As with ArrayEach(), you can use either use an inline function or a named function.

In this example, we have a simple collection of “favorites,” which we want to print to the screen.

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
// run structeach() with an inline function
structeach(favs, function(key,value) { 
    writeoutput('My favorite ' & key & ' is ' & value);

See the live example or grab the code from GitHub

Wrapping Up

While these two functions probably won’t garner the attention that other additions will, I think they’re pretty dang awesome. Not only are they a long time coming (honestly, how often do you loop over an array…like 20 times every hour!!), but they help to make developing with these basic data types just a bit easier as they more closely align methodologies in CF with those you’d find elsewhere.