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 ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access