Actionscript Course

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 attribute is used in <img> tag for the address of the image?
href src rel
<img src="http://coursesweb.net/imgs/webcourses.gif" width="191" height="63" alt="Courses-Web" />
Which CSS code hides the element on page?
display: none; display: inline; position: relative;
#id {
  display: none;
}
What instruction stops the execution of a while() or for() statement?
continue prompt() break
for(var i = 0; i< 8; i++) {
  if(i > 1) break;
  alert(i);
}
Indicate the function that can create a constant.
define() include() defined()
define("CONSTANT_NAME", "value");
echo CONSTANT_NAME;
Traverse Arrays - Multi-dimensional Array

Last accessed pages

  1. Common PHP Errors and Solutions (9661)
  2. Configuring Text (451)
  3. PHP OOP - Inheritance, class extends (5384)
  4. jQuery background position (5229)
  5. Display multiple groups of images (5375)

Popular pages this month

  1. Courses Web: PHP-MySQL JavaScript Node.js Ajax HTML CSS (470)
  2. Read Excel file data in PHP - PhpExcelReader (147)
  3. PHP Unzipper - Extract Zip, Rar Archives (145)
  4. SHA1 Encrypt data in JavaScript (121)
  5. Get and Modify content of an Iframe (108)
Chat
Chat or leave a message for the other users
Full screenInchide