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

Daily Test with Code Example

HTML
CSS
JavaScript
PHP-MySQL
Which tag adds a new line into a paragraph?
<b> <br> <p>
First line ...<br>
Other line...
Which CSS property can be used to add space between letters?
text-size word-spacing letter-spacing
#id {
  letter-spacing: 2px;
}
What JavaScript function can be used to get access to HTML element with a specified ID?
getElementById() getElementsByTagName() createElement()
var elm = document.getElementById("theID");
var content = elm.innerHTML;
alert(content);
Click on the "echo" correct instruction.
echo "CoursesWeb.net" echo "CoursesWeb.net"; echo ""CoursesWeb.net";
echo "Address URL: http://CoursesWeb.net";
Traverse Arrays - Multi-dimensional Array

Last accessed pages

  1. Dirt Dragons2 (81)
  2. Shooter Games (976)
  3. JavaScript Course - Free lessons (10465)
  4. Move image from an element /tag to another (1097)
  5. Courses Web: PHP-MySQL JavaScript Ajax HTML CSS Flash-AS3 (43806)

Top accessed pages

  1. Courses Web: PHP-MySQL JavaScript Ajax HTML CSS Flash-AS3 (43806)
  2. PHP-MySQL free course, online tutorials PHP MySQL code (33240)
  3. Read Excel file data in PHP - PhpExcelReader (33105)
  4. Get Attribute (ID, Class, Name, Title, Src) with jQuery (29933)
  5. PHP PDO - exec (INSERT, UPDATE, DELETE) MySQL (27253)