Sam Farmer

Growing up I never imagined I would play bass guitar for the Dave Matthews Band. And indeed it never happened.

10 Little Ones: The New Array Features

July 9, 2012

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.


3 Comments

How come the arraySort with function not documented? Would you pls bug Adobe to update the doc on arraySort()? Thanks.

By: Henry Ho 07/10/2012 1:04 PM
Henry, add a comment to the web page: http://help.adobe.com/en_US/ColdFusion/10.0/CFMLRef/WSc3ff6d0ea77859461172e0811cbec22c24-7f14.html

By: Raymond Camden 07/10/2012 4:49 PM
Some comments have been lost over the years due to moving hosts.

More


More blog entries that I have written.

Code coloring by PRISM.