O'Reilly logo

Actionscript Cookbook by Joey Lott

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

6.12. 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 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 ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required