A Closer Look at Array’s in ActionScript 3
I have been working with ActionScript for a long time. And I have to admit I don’t know if the reason I never noticed some of the functionality was it was added after I read the documentation, or if I just never looked close enough to pick it up. But never the less I find that I have been overlooking some gems that are built in and so I wanted to make sure that you are not overlooking them as well.
I have been writing a lot of ruby lately and I really like the syntax for looping over each item in an array. In Actionscript there are several ways to do this: for loops, for each, for each in, and while. But each of those takes more setup and management then I think should be required. What I really wanted was to be able to call:
array.each( functionName );
And that would call the function that I pass into the each method of the array, once for every element. Turns out this functionality is actually available on the Array class of Actionscript. Its called forEach. You can use it by providing the forEach method a reference to a function with a signature that looks like this `functionName(item:*, index:int, arr:Array):void`, where item is the current element in the array, index is the current items index and arr is a reference to the array being used. Some example code that uses this could look like:
1 2 3 4 5 6 7 8 9 10 11 12 13 | private var list:Array = ['One','Two','Three']; private function doSomethingToList():void { list.forEach(output); } private function output(item:*, index:int, arr:Array):void { trace(item); } doSomethingToList() |
That would cause each of the items on the array to be traced to the console. So much nicer than a for loop. But it is still a bit more code so you could shorten that up like:
1 2 3 4 | private function doSomethingToList():void { list.forEach(function(item:*, index:int, arr:Array):void{trace(item)}); } |
Or even chop it down a bit more using the rest array:
1 2 3 4 | private function doSomethingToList():void { list.forEach(function(... rest ):void{trace(rest[0])}); } |
So the morale of the story is that even if you think you know how something works, it can never hurt to take a look at the docs and confirm that. :) And if you like forEach make sure you check out: every, join, map, some and splice.
Ps. If anyone has a line on a really great code display plugin for WordPress, please drop me a line. I can’t get any to do much for me. Thanks
27 Jul 2008 Simeon

I had a similar thing the other day–found there’s a “indexOf()” method on arrays. Not that you NEED that, but it’s nice to know they have something built in like this. Saves you a few minutes and can make you design your code to leverage what’s already in AS… which makes your code more portable to other developers.
I prefer the following syntax but its probably just a preference thing.
for each(var item:String in List) {
trace(item);
}
Dan,
Actually, for each(var item:String in List) is not the same as Array.forEach. for each is a loop and forEach is a function. for each can be called on non arrays (objects) as well.
Danny
Hey Dan,
See my problem is that with which ever for each you define you only get an item or an index, but not both. And in that case the thing you are looping over is strongly typed not allowing for some reuse that could be fun.
But the syntax certainly requires less typing. :)
Hi
I had a similar revelation just recently, however, not related to the Array class. :D
I always manually converted Date objects to strings and the other way around if I had to…when I could simply use DateField.dateToString() and DateField.stringToDate().
So, about your post’s morale, yeah, that’s true. But I’m pretty sure no matter how long you work with AS3 you’ll always find something you missed…and then comes AS4 :D
About the code highlighter for wordpress: I use the one at ideathinking.com/wiki/index.php/WordPress:CodeHighlighterPlugin
Not sure how it handles mxml though…
Cheers!
Cristian
Hey Simeon,
You might want to check out SyntaxHighlighter : http://code.google.com/p/syntaxhighlighter/
Mark Walters made an AS3brush for that too: http://www.digitalflipbook.com/archives/2007/09/as3_syntax_high.php
Thanks Ronnie.
I had SyntaxHighlighter set up but it started butchering my xml with namespaces.
So I just set up a new one last week, but I had not updated my old posts.