Actionscript Course

Associative arrays use strings in place of index numbers, to associate the value of the elements. These indexes must be specified by the programmer.
- The syntax to create an associative array is:
var array_name:Array = new Array();
 array_name['index1'] = value1;
 array_name['index2'] = value2;
 array_name['index3'] = value3;
- You can add as many elements as you want.
- You can use either single or double quotes for "index".

You can access or change the value of a specific element of an associative array the same as with a numeric indexed array, using its index:   array_name['index'].

• The elements of an associative array can also be accessed with the dot (.) operator:   array_name.index   (like the proprietes of an object).

Here's an example in which we apply both methods:
// associative array, with 3 elements
var m_ar:Array = new Array();
 m_ar['site'] = 'coursesweb.net';
 m_ar['course'] = 'Adobe Flash';
 m_ar['tutorials'] = 'ActionScript';

// Access the element with index 'site'
trace(m_ar['site']);         // coursesweb.net

// Access another element, using the dot (.) operator
trace(m_ar.course);           // Adobe Flash

Traverse associative Arrays

You can traverse an associative array with the for..in, or for each..in loop.

Example with for..in

// associative array, with 3 elements
var m_ar:Array = new Array();
 m_ar['site'] = 'coursesweb.net';
 m_ar['course'] = 'Adobe Flash';
 m_ar['tutorials'] = 'ActionScript';

// create a "for..in" loop to traverse the "m_ar" array
for(var key:String in m_ar)
{
  // displays in Output the index and the value of the current item
  trace('Index='+ key+ ' - value='+ m_ar[key]);

  // You can execute any operations with "key" si "m_ar[key]"
}
- key is a variable which stores the "index" of the current element (you can use any name for the variable).
In Output will display:
Index=site - value=coursesweb.net
Index=tutorials - value=ActionScript
Index=course - value=Adobe Flash

Example with for each..in

// associative array, with 3 elements
var m_ar:Array = new Array();
 m_ar['site'] = 'coursesweb.net';
 m_ar['course'] = 'Adobe Flash';
 m_ar['tutorials'] = 'ActionScript';

// create a "for each..in" loop to traverse the "m_ar" array
for each(var elm in m_ar)
{
  // displays in Output the value of the current item (stored in elm)
  trace('valE - '+ elm);

  // You can execute any operations with "elm"
}

• Notice the diferences between "for..in" and "for each..in". The "for each..in" loop doesn't get the index of the element. The "elm" store the element itself.
In Output will display:
valE - Adobe Flash
valE - coursesweb.net
valE - ActionScript

Daily Test with Code Example

HTML
CSS
JavaScript
PHP-MySQL
Click on the tag that creates a cell in table
<tr> <span> <td>
<table></tr>
  <td>Cell-1</td><td>Cell-2</td>
</tr></table>
Indicate the CSS property used to specify a background image for an element
background-color background-image color
h3 {
  background-image: url("image.jpg");
}
What instruction can be used to parse all the array items?
for() [) object()
var arr = [1, "ab", "CoursesWeb.net"];
for(var i=0; i< arr.length; i++) { alert(arr[i]); };
Indicate the PHP instruction used to traverse an associative array.
for() foreach() if()
$arr =["k1"=>"v1", "k2"=>"v2", "k3"=>"v3");
foreach($arr AS $k => $v) { echo "<br/>". $k ." - ". $v; }
Associative Arrays in ActionScript

Last accessed pages

  1. Get Mouse coordinates inside a Div or an Image (16423)
  2. Get the value of multiple selected checkboxes with same name (7570)
  3. innerHTML and outerHTML to Get and Replace HTML content (30653)
  4. Register and show online users and visitors (39728)
  5. Recursive function to create Multi-Level Menu in PHP (11861)

Popular pages this month

  1. Courses Web: PHP-MySQL JavaScript Node.js Ajax HTML CSS (778)
  2. CSS cursor property - Custom Cursors (94)
  3. Read Excel file data in PHP - PhpExcelReader (74)
  4. The Mastery of Love (68)
  5. PHP-MySQL free course, online tutorials PHP MySQL code (67)