
This is the Title of the Book, eMatter Edition
Copyright © 2007 O’Reilly & Associates, Inc. All rights reserved.
270
|
Chapter 11: Arrays
To determine whether an object is an array, use the instanceof operator. For example:
var colors = ["red", "blue", "green"];
trace(colors instanceof Array); // Displays: true
Multidimensional Arrays
So far, we’ve limited our discussion to one-dimensional arrays, which are akin to a
single row or a single column in a spreadsheet. But what if we want to create the
equivalent of a spreadsheet with both rows and columns? We need a second dimen-
sion. ActionScript natively supports only one-dimensional arrays, but we can simu-
late a multidimensional array by creating arrays within arrays. That is, we can create
an array that contains elements that are themselves arrays (sometimes called nested
arrays).
The simplest type of multidimensional array is a two-dimensional array, in which
elements are organized conceptually into a grid of rows and columns—the rows are
the first dimension of the array, and the columns are the second.
Using a practical example, let’s consider how a two-dimensional array works. Sup-
pose we’re processing an order that contains three products, each with a quantity
and a price. We want to simulate a spreadsheet with three rows (one for each prod-
uct) and two columns (one for the quantity and one for the price). We create a sepa-
rate array for