ActionScript 3.0 Cookbook: Chapter 5, Arrays
Pages: 1, 2, 3, 4, 5, 6, 7, 8
However, if each member of the committee plays a special role, a standard array offers no way to indicate that. To address the issue, you can use an associative array. In some languages, this is called a hash table. In ActionScript, it is actually just an instance of the Object class. An associative array uses named elements rather than numeric indexes. The names used to refer to elements are often called keys or properties. The keys can give a meaningful context to the associated element value.
You can create an associative array in ActionScript by using object literal notation or adding elements to an object. Despite their name, you don't use the Array class to create associative arrays. The Array class provides methods and properties that work with number-indexed arrays only--and not with associative arrays. Associative arrays should be instances of the Object class. Technically, since the Object class is the base class for all ActionScript classes, all ActionScript objects can be used as associative arrays. However, unless you have some specific reason for using another class as an associative array, it is best to simply use the generic Object class.
One way you can create an associative array is by using object literal notation. With this technique, use curly braces ({ }) to enclose a comma-delimited list of keys and values, which are separated by a colon (:), as shown in the following example:
var memebers:Object = {scribe: "Franklin",
chairperson: "Gina",
treasurer: "Sindhu"};
You can also create an associative array using the following multiline technique with the Object constructor. Although the object literal notation is fine for creating small associative arrays in a single step, you should use the Object constructor technique for creating larger associative arrays. It improves readability and lets you add properties to an associative array by assigning the properties (keys) on subsequent lines. For example:
var members:Object = new Object( ); members.scribe = "Franklin"; members.chairperson = "Gina"; members.treasurer = "Sindhu";
Although using an Object constructor is more common, you can initialize the associative array object by using an empty object literal in place of the Object constructor:
var members:Object = {};
You can retrieve the values from an associative array in two ways. The first way is to access the elements using property notation (with the dot operator):
trace(members.scribe); // Displays: Franklin
The other option for retrieving values from an associative array is using array-access notation. To use array-access notation, reference the associative array followed by the array-access operator ([ ]). Within the array-access operator, you must use the string value of the name of the key you wish to access:
trace(members["scribe"]); // Displays: Franklin
Array-access notation is extremely useful in situations in which there are multiple keys with names in a sequence. This is because you can dynamically generate the key string value, whereas you cannot do this with property notation; for example:
var members:Object = new Object();
members.councilperson1 = "Beatrice";
members.councilperson2 = "Danny";
members.councilperson3 = "Vladamir";
for (var i:int = 1; i <= 3; i++) {
trace(members["councilperson" + i];
}
Array access notation is most frequently used when looping through every element in an associative array, as shown in Recipe 5.16.
You can use either the property notation or array-access notation to read or write the values of an associative array:
var members:Object = new Object( ); members["councilperson"] = "Ruthie"; trace(members.councilperson); // Displays: Ruthie members.councilperson = "Rebecca"; trace(members["councilperson"]); // Displays: Rebecca
Recipe 5.16 contains more details on accessing named elements of an associative array.
You want to loop through the elements of an associative array.
Use a for . . . in statement.
You iterate through the elements of integer-indexed arrays by using a for statement. However, named elements in associative arrays cannot be accessed by a numeric index, and the order of associative array elements is not guaranteed, regardless of the order in which the elements are added to the array. For that reason, there are also no methods to sort or reverse an associative array, or otherwise change its order.
This excerpt is from ActionScript 3.0 Cookbook. Well before Ajax and Windows Presentation Foundation, Macromedia Flash provided the first method for building "rich" web pages. Now, Adobe is making Flash a full-fledged development environment, and learning ActionScript 3.0 is key. That's a challenge for even the most experienced Flash developer. This Cookbook offers more than 300 solutions to solve a wide range of coding dilemmas, so you can learn to work with the new version right away.
Fortunately, you can loop through the enumerable elements of an associative array by using a for . . . in statement. This statement iterates through all the readable properties of the specified object. The syntax for a for . . . in statement is as follows:
for (key in object) {
// Actions
}
The for . . . in statement doesn't require an explicit update statement because the number of loop iterations is determined by the number of properties in the object being examined. Note that key is a variable name that will be used to store the property name during each iteration, not the name of a specific property or key. On the other hand, object is the specific object whose properties you want to read. For example:
var members:Object = new Object( );
members.scribe = "Franklin";
members.chairperson = "Gina";
members.treasurer = "Sindhu";
// Use a for . . . in statement to loop through all elements.
for (var sRole:String in members) {
// Displays:
// treasurer: Sindhu
// chairperson: Gina
// scribe: Franklin
trace(sRole + ": " + members[sRole]);
}
When you use a for . . . in statement, you must use array-access notation (square brackets) with the associative array. If you try to use property notation (with the dot operator) it won't work properly. This is because the value that is assigned to the key iterator variable is the string name of the key, not the key's identifier.
A for . . . in loop does not display all built-in properties of an object. For example, it displays custom properties added at runtime, but it does not enumerate methods of built-in objects, even though they are stored in object properties.
Recipe 5.2
Skip to any available Digital Media Help Center chapter of ActionScript 3.0 Cookbook: |