Traversing Arrays
The elements of an Array can easily be parsed with one of these loop instructions:
for(), while(), and
for each..in.
Traversing an Array with one of these instructions enables you to access each element in an Array, in their order.
Here's an example with each of them:
Using for()
var m_ar:Array = [3, 8, 32];
var nrel:int = m_ar.length; // get the number of elements in "m_ar"
// define a "for()" loop to traverse the "m_ar" array
for(var i:int=0; i<nrel; i++)
{
// doubles the current element value
m_ar[i] *= 2;
}
// uses trace() to check the values in "m_ar"
trace(m_ar); // 6,16,64
-
var i:int=0; - sets the starting value for the index. This index specifies the element from which the for() loop begins to traverse the array (
you can set any value for the starting index).
-
i<nrel - makes the for() loop to go through to the last element ("nrel" contains the number of items in array).
-
i++; - tells the for() instruction to increment the value of "i" by 1, at the end of each loop, to get the next element.
Using while()
var m_ar:Array = [3, 8, 32];
var nrel:int = m_ar.length; // get the number of elements in "m_ar"
var i:int = 0; // defines the starting index
// define a "while()" loop to traverse the "m_ar" array
while(i<nrel)
{
// doubles the current element value
m_ar[i] *= 2;
i++; // increments the index to go to the next item
}
// uses trace() to check the values in "m_ar"
trace(m_ar); // 6,16,64
Using for each..in
var m_ar:Array = [3, 8, 32];
// define a "for each..in" loop to traverse the "m_ar" array
for each(var elm:Number in m_ar)
{
// doubles the value of the current element, which is stored in "elm"
elm *= 2;
// you can perform operations with "elm", its value not affect the array
}
// uses trace() to check the values in "m_ar"
trace(m_ar); // 3,8,32
- Notice that in this case the array is not affected, "elm" only gets the value of the element, and you can perform operations with it.
Multi-dimensional Array
The element in an array can be another array. In this case we are dealing with a multi-dimensional array.
Let's see an exampe:
// define a simple array (which will be included in the next array)
var arr:Array = [88, 'ActionScript'];
// define an array with 3 elements (the third item is the "arr" variable)
var m_ar:Array = ['tutorials', 'coursesweb.net', arr];
// checks the values of the main array and of the third element
trace(m_ar); // tutoriale,coursesweb.net,88,ActionScript
trace(m_ar[2]); // 88,ActionScript
- The array in the third element ("
arr" which contains
[88, 'ActionScript']) is called "
nested array".
Accessing elements in a Nested Array
To access the elements of a nested array, use the following syntax:
array_name[i1][i2]
- "i1" is the index associated to the nested array, "i2" is the index of the element in the nested array.
Example:
// define a simple array (which will be included in the next array)
var arr:Array = [88, 'ActionScript'];
// define an array with 3 elements (the third item is the "arr" variable)
var m_ar:Array = ['tutorials', 'coursesweb.net', arr];
// stores in a variable the second element in the nested array
var test:* = m_ar[2][1];
// check the value of the "test"
trace(test); // ActionScript
Traversing a multi-dimensional Array
To traverse a multi-dimensional Array, including the nested arrays, you should use nested loop instructions, like in the following example.
// define a simple array
var arr:Array = [88, 'ActionScript'];
// define an array with 3 elements (the third item is the "arr" variable)
var m_ar:Array = ['tutorials', 'coursesweb.net', arr];
var nrel = m_ar.lengt; // get the number of elements in "m_ar"
// create 2 nested for() loops to access each item in the multi-dimensional array
for(var i:int=0; i<nrel; i++) {
var elm:* = m_ar[i]; // get the current element in "m_ar"
// if "elm" is an Array, define another for() loop to traverse it
// else, outputs its value
if(elm is Array) {
for(var i2:int=0; i2<elm.length; i2++) {
trace(elm[i2]); // output the value of the current element in nested array
}
}
else {
trace(elm);
}
}
- The "
(elm is Array)" returns true if "elm" is an Array data type, else, returns false.
- The first for() loop traverses the main array. If the current element is an array, the nested for() instruction is executed to access its elements (you can use any loop instruction, while(), for..in).
The example above will display in Output panel:
tutorials
coursesweb.net
88
ActionScript