Additional Array Methods


So far we have gone over methods for adding and removing elements. Now we will go over some other array methods for manipulating elements within an array.

The toString Method

Oftentimes, you might want to set an array equal to a variable, but when you set a variable equal directly to an array, the script simply copies the array over to that variable and stores each element as its own element. We'll use the toString method, which you saw before in Chapter 9, to convert an entire array to one string, with each element separated by commas:

 var my_array:Array = new Array("fName","lName"); var another_array:Array = my_array; var myVariable:String = my_array.toString(); trace(another_array[0]); trace(myVariable[0]); //output: fName //        undefined 

This example shows that when we copied my_array into another_array, an exact copy of the original array was created. Then we copied the same array to myVariable, but attached the toString method to it. When we tried to trace a singular element out of myVariable, undefined was returned. So now let's drop the index of myVariable and see what happens:

 var my_array:Array = new Array("fName","lName"); var another_array:Array = my_array; var myVariable:String = my_array.toString(); trace(another_array[0]); trace(myVariable); //output: fName //        fName,lName 

Notice that the elements are separated with commas when the array becomes a string. But what if you want to separate each element with some other character? The join method can accomplish this.

The join Method

Similar to the toString method, the join method converts all elements in an array to one string to place in a variable. Unlike the toString method, the join method separates each element the way you want it to. Again, just call this method on an array like you would any other method and then place whatever string you want to separate the elements with between the parentheses. Here's an example:

 var my_array:Array = new Array("fName","lName","age","location"); var myVariable:String = my_array.join(""); trace(myVariable); //output: fNamelNameagelocation 

Alternatively, you can leave the parentheses blank, which causes join to act just like the toString method:

 var my_array:Array = new Array("fName","lName","age","location"); var myVariable:String = my_array.join(); trace(myVariable); //output: fName,lName,age,location 

You can even put in an expression, as shown here:

 var my_array:Array = new Array("fName","lName","age","location"); var myVariable:String = my_array.join(2+2); trace(myVariable); //output: fName4lName4age4location 

Now let's look at another method for arraysthe slice method.

The slice Method

Like the splice method, the slice method can grab elements from an array and place them into a new array. Unlike the splice method, however, the slice method does not affect the original array. Here's an easy way to think of these methods: The splice method is like cutting, and the slice method is like copying.

The syntax for the slice method is the same as the splice methodyou can set the starting point and how many elements you want to copy. Here's an example:

 var my_array:Array = new Array("fName","lName","age","location"); var another_array:Array = my_array.slice(2); trace(another_array); trace(my_array); //output: age, location //        fName, lName, age, location 

The slice method copies the elements, starting with the declared index, to the last element of the original array and places them in a new array without affecting the original array.

You can also set the ending index of the elements you wish to copy:

 var my_array:Array = new Array("fName","lName","age","location"); var another_array:Array = my_array.slice(2,3); trace(another_array); //output: age 

So far, these methods have removed, added, and shifted elements. Now let's change the order of them with the reverse method.

The reverse Method

The reverse method is exactly what it sounds likeit's a method for reversing the order of all the elements in an array. Once an array is created, you can call the reverse method on it, like so:

 var my_array:Array = new Array("fName","lName","age","location"); my_array.reverse(); trace(my_array); //output: location, age, lName, fName 

The reverse method is used mainly for reversing already sorted arrays. The next section shows you how sorting is accomplished.




Macromedia Flash Professional 8 Unleashed
Macromedia Flash Professional 8 Unleashed
ISBN: 0672327619
EAN: 2147483647
Year: 2005
Pages: 319

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net