Recipe 5.15.
Creating an Associative Array
Problem
You
want to create an array that
uses named elements instead
of numbered indexes.
Solution
Create an associative array.
Discussion
When working with sets of data in which each
element has a specific meaning or importance, a typical,
number-indexed array doesn't
always suffice.
For example, if you are working with a set of
data such as the names of members of a committee, a number-indexed
array is sufficient:
var aMembers:Array = new Array("Franklin", "Gina", "Sindhu");
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 onlyand 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
See Also
Recipe
5.16 contains more details on accessing named elements of an
associative array.
|