My Blog

Flash IconUsing Arrays in AS3 is a must its also important to have a good understand of best practices here are some best practices and tips & tricks that I use.

 

What is an Array?

An array is a collection of data, or in other words, a list of values.

 

Creating an Array

 //The official way  
var myArray:Array = new Array();

//Known as the array literal
var myArray:Array = [];

 

Index

Each item in an Array is assigned a numeric index, which uniquely identifies it and indicates the position of that item within the list.

 

Tip 1 - Off By One Error

ActionScript is a zero-based language, meaning that the first item in an Array has an index of 0 (zero).

 

Tip 2 - Be Specific

Always be specific when creating arrays

//Bad
var arr:Array = new Array();

//Good
var letters:Array = new Array( );

 

Tip 3 - Push is Good

Use array push to add items

var letters:Array = new Array( );
letters.push("a", "b", "c", "d");

 

Tip 4 - Adding Items

Use array unshift when adding items to the beginning and push to the end of the array

var letters:Array = new Array( );
letters.push("a", "b", "c", "d");
letters.unshift("z");

 

Tip 5 - Splice

Use array splice, shift and pop to remove items

var letters:Array = ["a", "b", "c", "d"];    
//Remove one element from letters starting at index 1.
letters.splice(1, 1);

//Remove the first element and display its value.
trace(letters.shift( ));

//Remove the last element and display its value.
trace(letters.pop( ));

 

Tip 6 - Total Items

Use Array length to get the total items of an array.

trace(myArray.length);

 

Tip 7 - Loop

Looping through an array

var letters:Array = ["a", "b", "c"];
for (var i:int = 0; i < letters.length; i++) {
  trace(letters[i]);
}

 

Tip 8 - ArrayUtilities

Use the ArrayUtilities class for finding and matching items in an array

var letters:Array = ["a", "b", "c", "d"];
trace(ArrayUtilities.findMatchIndex(letters, "b"));
trace(ArrayUtilities.findMatchIndex(letters, "r"));

 

Tip 9 - CASAlib

Use CASAlib ActionScript library http://casalib.org/ which has addItemsAt and removeItem

//addItemsAt
var alphabet:Array = new Array("a", "d", "e");
var parts:Array    = new Array("b", "c");

ArrayUtil.addItemsAt(alphabet, parts, 1);
trace(alphabet);

//removeItem
var numberArray:Array = new Array(1, 2, 3, 7, 7, 7, 4, 5);
trace("Removed " + ArrayUtil.removeItem(numberArray, 7) + " items.");
trace(numberArray);

 

Tip 10 - Concat

Use concat to copy an array

var letters:Array = ["a", "b", "c"];
//Create an independent copy of letters using concat( ),
//which returns a new array.
var newLetters:Array = letters.concat( );

 

Tip 11 - Sort

Use the sort method for sorting elements of an array

var words:Array = ["tricycle", "relative", "aardvark", "jargon"];
words.sort( );
trace(words); //Displays: aardvark,jargon,relative,tricycle

 

Tip 12 - For Each

Thanks to Elad Elrom for reminding me about the forEach method

var myFunction:Function = function(obj:Object):void {
         //your code here
         };
     myArray.forEach(myFunction, me);
To Delicious, Stumbleupon, Digg, And More
 

Comments

There are 5 comments (+ Add Comment)
  1. 223  

    Nov 06th, 2009 at 03:11 pm

    Nice writeup, don’t forget “foreach” method:

    forEach(callback:Function, thisObject:* = null):void

  2. 224 http://www.almogdesign.net  

    Nov 06th, 2009 at 03:11 pm

    Great point totally forgot about that thanks

  3. 406 http://www.mogobe.com  

    Jan 04th, 2010 at 04:01 pm

    you have a mistake at :
    letters.push(”a”, “b”, “c”, “d”);
    letters.unshift(”z”);
    this 2 are not the same.
    push add’s a value at the end of the array while
    unshift add’s a value at the beginning of the array,
    http://livedocs.adobe.com/flash/9.0/ActionScriptLangRefV3/Array.html#unshift()

  4. 410 http://www.mogobe.com  

    Jan 05th, 2010 at 09:01 am

    you wrote:
    Tip 4 - Adding Items
    Use array unshift when adding items to the end, and push to the being
    var letters:Array = new Array( );
    letters.push(”a”, “b”, “c”, “d”);
    letters.unshift(”z”);
    but this should be the opposite :
    Use array unshift when adding items to the beggining, and push to the end of the Array.
    have a great day
    Peleg

  5. 411 http://www.almogdesign.net  

    Jan 05th, 2010 at 06:01 pm

    Thanks Peleg typing mistake fixed it

Post Comment

    • Please keep comments related to topic. And be nice, don't spam!
    • Basic HTML tags are allowed:
      <a href> <abbr> <acronym> <blockquote> <code> <em> <strike> <strong>
    • Note: un-related or spam comments will be deleted.