Tips & Tricks ActionScript 3 Arrays
Using 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
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
var arr:Array = new Array();
//Good
var letters:Array = new Array( );
Tip 3 – Push is Good
Use array push to add items
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
letters.push("a", "b", "c", "d");
letters.unshift("z");
Tip 5 – Splice
Use array splice, shift and pop to remove items
//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.
Tip 7 – Loop
Looping through an array
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
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
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
//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
words.sort( );
trace(words); //Displays: aardvark,jargon,relative,tricycle
Tip 12 – For Each
Thanks to Elad Elrom for reminding me about the forEach method
{
//your code here
};
myArray.forEach(myFunction, me);











Just finished a new blog post Tips & Tricks ActionScript 3 Arrays http://bit.ly/2ZZ1rH
Nice writeup, don’t forget “foreach” method:
forEach(callback:Function, thisObject:* = null):void
Great point totally forgot about that thanks
Tips & Tricks #ActionScript 3 Arrays http://bit.ly/3roJF6
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()
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
Thanks Peleg typing mistake fixed it
#WBG340 Tips and Tricks for Flash ActionScript 3.0 Arrays. http://t.co/3To1YMnZ