Adobe FLEX 2-PROGRAMMING ACTIONSCRIPT 3.0 Manual page 242

Programming actionscript 3.0
Table of Contents

Advertisement

Associative array with an indexed array
To make the individual arrays easier to access, you can use an associative array for the days of
the week and an indexed array for the task lists. Using an associative array allows you to use
dot syntax when referring to a particular day of the week, but at the cost of extra run time
processing to access each element of the associative array. The following example uses an
associative array as the basis of a task list, with a key and value pair for each day of the week:
var masterTaskList:Object = new Object();
masterTaskList["Monday"] = ["wash dishes", "take out trash"];
masterTaskList["Tuesday"] = ["wash dishes", "pay bills"];
masterTaskList["Wednesday"] = ["wash dishes", "dentist", "wash dog"];
masterTaskList["Thursday"] = ["wash dishes"];
masterTaskList["Friday"] = ["wash dishes", "clean house"];
masterTaskList["Saturday"] = ["wash dishes", "wash car", "pay rent"];
masterTaskList["Sunday"] = ["mow lawn", "fix chair"];
Dot syntax makes the code more readable by making it possible to avoid multiple sets of
brackets.
trace(masterTaskList.Wednesday[1]); // output: dentist
trace(masterTaskList.Sunday[0]);
// output: mow lawn
You can iterate through the task list using a
loop, but you must use bracket notation
for..in
instead of dox syntax to access the value associated with each key. Because
is
masterTaskList
an associative array, the elements are not necessarily retrieved in the order that you may
expect, as the following example shows:
for (var day:String in masterTaskList)
{
trace(day + ": " + masterTaskList[day])
}
/* output:
Sunday: mow lawn,fix chair
Wednesday: wash dishes,dentist,wash dog
Friday: wash dishes,clean house
Thursday: wash dishes
Monday: wash dishes,take out trash
Saturday: wash dishes,wash car,pay rent
Tuesday: wash dishes,pay bills
*/
242
Working with Arrays

Advertisement

Table of Contents
loading

This manual is also suitable for:

Flex

Table of Contents