Adobe ColdFusion 10 brings some improvements to manipulating arrays.
In this example we have two arrays called one and two. A new argument has been added to arrayAppend called arrayConcat that when true will append two arrays together.
one = ["a","c","e"]; two = ["b","d"]; arrayAppend( one, two, true ); writeDump( var=one, label="one after arrayAppend");
one after arrayAppend - array | |
---|---|
1 | a |
2 | c |
3 | e |
4 | b |
5 | d |
Array one now has all the values. The order seems a little off though. Enter arraySort which as its second argument takes a closure/function where you can write your own sort logic. Basic sorting is straightforward and its nice to have the flexibility to extend it when needed.
arraySort( one, function( a, b ) { return ( a > b ); }); writeDump( var=one, label="one after arraySort");
one after arraySort - array | |
---|---|
1 | a |
2 | b |
3 | c |
4 | d |
5 | e |
In the next example we'll filter the array by writing a closure to return an array with just the letters that are in my name.
filtered = arrayFilter( one, function( val ) { if ( reFindNoCase("s|a|m", val ) ) { return true; } else { return false; } }); writeDump( var=filtered, label="filtered");
filtered - array | |
---|---|
1 | a |
Slicing of arrays has also been added with the arraySlice function.
sliced = arraySlice( one, 1, 1 ); writeDump(sliced);
array | |
---|---|
1 | a |
last = arraySlice( one, -1, 1 ); writeDump(last);
array | |
---|---|
1 | e |
Where it makes send these functions also have list and struct equivalents.
By: Henry Ho 07/10/2012 1:04 PM