Recipe 6.12 Creating an Associative Array

6.12.1 Problem

You want to create an array that uses named elements instead of numbered indexes.

6.12.2 Solution

Create an associative array.

6.12.3 Discussion

When you are working with sets of data in which each element has a specific meaning or importance, a typical, number-indexed array does not 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:

members = 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 (sometimes called a hash table or simply an object). 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 using object literal notation (which will be shown shortly) or by 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 must be instances of the Object class. Technically, since the Object class is the base class for all ActionScript classes, all ActionScript objects are associative arrays. You can use this fact to your advantage when working with movie clips (see Recipe 7.10). But when you are creating an object explicitly for the purpose of forming an associative array, you should use an instance of the generic Object class.

One way you can create an associative array is by using object literal notation. With this technique you use curly braces ({ }) to enclose a comma-delimited list of keys and values, which are separated by a colon (:). Here is an example:

members = {scribe: "Franklin", chairperson: "Gina", treasurer: "Sindhu"};

For readability, this can be written as:

members = {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:

members = 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 using an empty object literal in place of the Object constructor:

members = {};

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:

members = new Object(  );
members.councilperson1 = "Beatrice";
members.councilperson2 = "Danny";
members.councilperson3 = "Vladamir";

for (var i = 1; i <= 3; i++) {
  trace(members["councilperson" + i];

You can use either the property notation or array-access notation to read or write the values of an associative array:

members = new Object(  );
members["councilperson"] = "Ruthie";
trace(members.councilperson);         // Displays: Ruthie
members.councilperson = "Rebecca";
trace(members["councilperson"]);      // Displays: Rebecca

6.12.4 See Also

Recipe 6.13 contains more details on accessing named elements of an associative array.

    Part I: Local Recipes
    Part II: Remote Recipes