Actionscript 3 Designer Toolkit

More design, less programming

Actionscript 3 Designer Toolkit header image 2

Using Arrays in Actionscript 3: Part 2 – Associative Arrays

November 7th, 2009 · 2 Comments · Tutorials

Associative arrays are the more sophisticated sibling of integer-indexed arrays. Associative arrays, in addition to having integer-indexed elements, also have named data defined within each indexed element. So that the first element, at index=0, might contain data such as firstname: “Joseph”, lastname: “Jones” and employeeID: 345421. Associative arrays serve as extremely useful containers for data loaded from external XML files.

The code below declares a new instance of the array class named students and then uses the push method to add 6 members to the array. Note the curly braces that wrap the comma seperated data. The sortOn() method is then used to sort the array by grade and then the reverse() is used to reverse the sort. Finally, a for… next is used to display data from the array.

 
// declare new array students
var students:Array = new Array();
 
// add entries to array
students.push( { firstname: 'Jennifer', lastname: 'Risen',   subject: 'History', grade: 'C' } );
students.push( { firstname: 'Roger',    lastname: 'Searing', subject: 'English', grade: 'B' } );
students.push( { firstname: 'Michael',  lastname: 'Brown',   subject: 'English', grade: 'A' } );
students.push( { firstname: 'Robert',   lastname: 'Kliener', subject: 'Math',    grade: 'C' } );
students.push( { firstname: 'Michelle', lastname: 'Sommer',  subject: 'English', grade: 'A' } );
students.push( { firstname: 'Janice',   lastname: 'Wilson',  subject: 'English', grade: 'D' } );
 
// sort the list by grade
students.sortOn( 'grade' );
 
// reverse the order
students.reverse();
 
// loop through list showing first, last names & grade
for ( var i:uint=0; i<students.length; i++ )
{
	trace( students[i].firstname + ' ' + students[i].lastname + ':  ' + students[i].grade );
}

The following code employs another for… next loop to locate the array element matching with a last name matching the value stored in the variable inquiry.

 
// declare var containing a student's last name
var inquiry:String = 'Searing';
 
// loop through list until last name is found
for ( var j:uint=0; j<students.length; j++ )
{
	if ( students[j].lastname == inquiry )
	{
		trace( students[j].lastname + ':  ' + students[j].grade );
		break;
	}
}

In the code below, an alternative method is used to transfer data into an associative array. A data object is created and to hold student1’s first, last names and subject. Then a new array seniors is created and the object’s data is transferred to the array using the push method.

 
// declare an object
var student1:Object = new Object();
 
// add data to the object
student1.firstname = 'Chuck';
student1.lastname  = 'Davis';
student1.subject   = 'Actionscript 3';
 
trace( student1 ); // returns type object Object
 
trace( student1.lastname );  // returns Davis
 
// declare new array and add data from object student1
var seniors:Array = new Array();
seniors.push( student1 );
 
trace( seniors[0].lastname );  // returns Davis

If you found this post useful, please consider a Retweet.

Post to Twitter

Tags: ····

2 Comments so far ↓

  • Bruce

    I’m not a CS major or anything, but I’m pretty sure that what you have described here is not an associative array… it’s a just a normal array which happens to contain objects.

    Technically, an associative array is an array that uses something other than a simple integer key… eg. uses string values as keys
    array["a"] = “value”
    array["b"] = “value”

    In Actionscript and Javascript an associative array is an Object

  • Shane

    I agree with Bruce.

    This article is not about associative array.
    It is more of an explanation on how to use objects.

Leave a Comment

Spam Protection by WP-SpamFree