HTML and CSS Reference
In-Depth Information
This example creates a two dimensional 3 × 3 array. Each array isn't required to be the
same size; this example was just coded that way. Accessing the elements of a two-dimensional
array is much the same as accessing a one-dimensional array, but you use two indexes:
multiArray[1][2] = 'ball sport';
This example assigns a value to the first index of the first dimension and the second index
of the second dimension, as illustrated in Figure 2-1.
FIGURE 2-1 Layout of a two-dimensional array
Because arrays are objects, they expose a number of powerful methods to make working
with them easier. The following sections explain each available method and property.
Using the length property
The length property provides information on how long the array is—that is, how many ele-
ments the array has allocated at the time the property is evaluated. This property is useful for
situations in which you need to iterate over an array or to show users how many items are in
the array at a specific point in time, such as in a queue. The following example shows how to
access the length property:
var anArray = new Array(5);
Many functions enable you to manipulate array contents quickly and easily.
Some array methods affect the Array object directly, whereas other methods return a new
Array object. For the exam, you must understand when each case is applicable.
Using the concat method
The concat method combines two or more arrays into one array:
var sports = new Array( 'football', 'cricket', 'rugby', 'tennis', 'badminton');
var moreSports = new Array('soccer', 'basketball', 'hockey');
var combinedSports = sports.concat(moreSports);
Search WWH ::

Custom Search