Blog

If I have a thousand ideas and only one turns out to be good, I am satisfied. Alfred Bernhard Nobel

Tips & Tricks ActionScript 3 Arrays

5th November 2009 ActionScript 3, Blog

FlashUsing 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);

Share To Deliciious, Digg and More
8
appreciated comments
  1. 1
    almogdesign November 6th, 2009 on 10:28

    Just finished a new blog post Tips & Tricks ActionScript 3 Arrays http://bit.ly/2ZZ1rH

  2. 2
    Elad Elrom November 6th, 2009 on 15:20

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

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

  3. 3
    Almog November 6th, 2009 on 15:54

    Great point totally forgot about that thanks

  4. 4
    Arulmurugan November 26th, 2009 on 18:16

    Tips & Tricks #ActionScript 3 Arrays http://bit.ly/3roJF6

  5. 5
    peleg January 4th, 2010 on 16:09

    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()

  6. 6
    peleg January 5th, 2010 on 09:45

    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

  7. 7
    Almog January 5th, 2010 on 18:01

    Thanks Peleg typing mistake fixed it

  8. 8
    Heather Teter November 23rd, 2011 on 01:30

    #WBG340 Tips and Tricks for Flash ActionScript 3.0 Arrays. http://t.co/3To1YMnZ

Leave a Comment