flash language_api_array_class

Enrichment of the Array class

The Array class benefits from new methods in ActionScript 3 easing data manipulation. The forEach method allows you to loop within an array:

// Array.forEach processes simple navigation
// on each element with the help of a specific function
var names:Array = [ "bobby", "willy", "ritchie" ];
function navigate ( element:*, index:int, array:Array ):void
{
trace ( element + " : " + index + " : " + array);
}
/* display :
bobby : 0 : bobby,willy,ritchie
willy : 1 : bobby,willy,ritchie
ritchie : 2 : bobby,willy,ritchie
*/
names.forEach( navigate );

All of these new methods function on the same principal. A navigation function is passed in a parameter in order to repeat and to treat the data within an array.

The every method executes the navigation function until it or the element passed over send back a false. It is therefore very simple to determine if an array contains the necessary values. In the following code we are testing to see if the array data only contains numbers:

var data:Array = [ 12, "bobby", "willy", 58, "ritchie" ];
function navigate ( element:*, index:int, array:Array ):Boolean
{
return ( element is Number );
}
var array Numbers:Boolean = data.every ( navigate );
// display : false
trace( array Numbers );

The map method allows for the creation of an array relative to the return of the navigation function. In the following code, we apply formatting to the names data array.

A new formatted names array is created:

var names:Array = ["bobby", "willy", "ritchie"];
function navigate ( element:*, index:int, array:Array ):String
{
return element.charAt(0).toUpperCase()+element.substr(1).toLowerCase();
}
// we create an array from the return of the navigate function:
var namesFormated:Array = names.map ( navigate );
// display : Bobby,Willy,Ritchie
trace( namesFormated );

The map method doesn’t allow you to filter data. All the array source data are therefore placed within the generated array.

If we want to filter data we need to call the filter method. In the following code we filter minor users and obtain an array of major users:

var users:Array = [ { name : "Bobby", age : 18 },
{ name : "Willy", age : 20 },
{ name : "Ritchie", age : 16 },
{ name : "Stevie", age : 15 } ];
function navigate ( element:*, index:int, array:Array ):Boolean
{
return ( element.age >= 18 );
}
var majorUsers:Array = users.filter ( navigate );
function browse ( element:*, index:int, array:Array ):void
{
trace ( element.name, element.age );
}
/* display :
Bobby 18
Willy 20
*/
majorUsers.forEach( browse );

The some method allows you to know if an element exists at least once in an array. The navigation function is executed until it or an element of the array come back as true:

var users:Array = [ { name : "Bobby", age : 18, sex : "M" },
{ name : "Linda", age : 18, sex : "F" },
{ name : "Ritchie", age : 16, sex : "M"},
{ name : "Stevie", age : 15, sex : "M"} ]
function navigate ( element:*, index:int, array:Array ):Boolean
{
return ( element.sex == "F" );
}
// is there a woman amongst the users in the array?
var resultat:Boolean = users.some ( navigate );
// display : true
trace( result );

The indexOf methods and lastIndexOf also appear in the Array class which allow you to search to see if an element exists and to obtain its position:

var users:Array = [ "Bobby", "Linda", "Ritchie", "Stevie", "Linda" ];
var position:int = users.indexOf ("Linda");
var positionFin:int = users.lastIndexOf ("Linda");
// display : 1
trace( position );
// display : 4
trace( positionFin );

The indexOf methods and lastIndexOf allow you to search for both primitive type and composite type elements. We can also search the presence of references within the array:

var myClip:DisplayObject = new MovieClip();
var myOtherClip:DisplayObject = new MovieClip();
// a reference to the myClip clip is placed in the array
var arrayReferences:Array = [ myClip ];
var position:int = arrayReferences.indexOf (myClip);
var otherPosition:int = arrayReferences.lastIndexOf (myOtherClip);
// display : 0
trace( position );
// display : -1
trace( otherPosition );

We touch on these methods more in later articles.

Worth remembering

  • Remember to use the new methods of the Array class in order to more easily treat the array data.







Adobe Training center


Mediabox Training Centre © 2000 - 2008 All rights reserved.
Adobe Authorized Training Centre. State convention under number 25 14 02167 14.
Mediabox : SARL au capital de 62.000€ - Activity number: 25 14 02167 14 - SIRET : 493 716 468 00027
MEDIABOX, 102 Avenue des Champs Elysées, 75008 PARIS - Tel. +33(0)2.31.91.96.89 - Fax. +33(0)2.72.68.56.42