ActionScript Programming/PartI/Chapter 3/Arrays/Methods

From Wikibooks, open books for an open world
Jump to: navigation, search

Data types[edit]

Arrays[edit]

Methods[edit]

Mfas image017.jpg
Method Description
concat( <array1>, <array2> ); returns new array.
join( <separator> ); returns the array separated with the following separator.
pop( ); returns the value of the last element.
push( <element1>, <element2> ); returns the length of the new array.
reverse( ); returns nothing.
shift( ); returns the value of the first element.
slice( <start>, <end> ); returns a new array with the sliced elements.
sort( ); returns nothing.
sortOn( ); returns nothing
splice( <start>, <delete count>, <element1>, <element2>, <…> ) returns nothing.
toString( ); returns the whole array in string.
unshift( <element1>, <element2>, <…>); returns array’s length.
concat[edit]

Array.concat( <array1>, <array2> );

The method “concat” combines arrays with each other. For example this code combines two arrays.

1.   var array1 = new Array("a", "b", "c");
2.   var array2 = new Array("d", 5, "f");
3.   var array3 = new Array;
4.   
5.   array3 = array1.concat(array2);
6.   trace(array3);

The result will be:

a,b,c,d,5,f
join[edit]

Array.join( <separator> );

The method join is used to tell the program to return the array values separating them with current separator. For example this code declares an array variable “array1” with 3

1.   var array1 = new Array("a", "b", "c");
2.   
3.   trace(array1.join(" ; "));
4.   trace(array1.join(" + "));
5.   trace(array1.join(" zz "));
6.   trace(array1.join());

elements: “a”, “b”, “c” and then trace the array values by using the current separator. In line 3 we use “ ; ” separator. This means that the return value will be “a ; b ; c”. In line 4 we use “ + “ separator. So the return value will be “a + b + c”. In line 5 we use “ zz “ separator. Return value will be “a zz b zz c”. If you don’t use any separator, then the default separator (“,”) is used. So the traced value in line 6 will be “a,b,c”.

pop[edit]

Array.pop( );

This method removes the last element from the array and then returns the name of the element. This example will show how “pop” method works.

1.   var myarray = new Array(1, 2, 3, 4);
2.   var popped;
3.   
4.   popped = myarray.pop();
5.   trace(popped);
6.   trace(myarray);

Line 1 declares “myarray” array with 4 elements: 1, 2, 3, 4. Line 2 declares “popped” variable without assigning any data type. In line 4 we pop “myarray” and write the last element, which is popped to the “popped” variable. In line 5 we trace variable “popped” to see the name of the element which was popped, and in line 6 we trace “myarray”. The Output window must trace the following:

4
1,2,3
push[edit]

Array.push( <element1>, <element2> );

This method adds one or more elements to the end of the array and then returns the length of the new array.

1.   var myarray = new Array(1, 2);
2.   var newlength;
3.   
4.   newlength = myarray.push(3, 4);
5.   trace(newlength);
6.   trace(myarray);

The first line declare “myarray” array with two elements: 1, 2. The second line declares “newlength” variable in which we will keep the new length of the array. In line 4 we add two elements 3 and 4 to the end of “myarray” using “push” method and then write the new length of the array in “newlength”. The following must be traced in Output window:

4
1,2,3,4
reverse[edit]

Array.reverse( );

“reverse” method reverses the array.

1.   var myarray = new Array(1, 2, 3, 4, 5);
2.   
3.   trace(myarray);
4.   myarray.reverse();
5.   trace(myarray);

The first line creates an array with 5 elements: 1, 2, 3, 4, 5. Line 3 traces “myarray”, then line 4 reverses the array and line 5 again traces our array. The output must be:

1,2,3,4,5
5,4,3,2,1
shift[edit]

Array.shift( <start>, <end> );

This method is similar to method “pop” but removes not the last, but the first element and then returns the value of it.

1.   var myarray = new Array(1, 2, 3, 4);
2.   var shifted;
3.   
4.   shifted = myarray.shift();
5.   trace(shifted);
6.   trace(myarray);

Line 1 declares “myarray” array with 4 elements: 1, 2, 3, 4. Line 2 declares “shifted” variable without assigning any data type. In line 4 we shift “myarray” and write the value of the first element in “shifted” variable. In line 5 we trace variable “shifted” to see the name of the element which was shifted, and in line 6 we trace “myarray”. The output must be:

1
2,3,4
slice[edit]

Array.slice( <start>, <end> );

The method “slice” copies the elements of one array and then returns them as an array. To use this method you must give it the start and the last indexes of the elements.

1.   var array1 = new Array(1, 2, 3, 4, 5);
2.   var array2 = new Array();
3.   
4.   array2 = array1.slice(1, 4);
5.   trace(array2);

For example the following example copies the second, third and forth elements and returns them to “array2”. The second parameter ( “end” ) which you give to “slice” method is not sliced. In the previous example we set the first parameter to 1 and the second to 4. If you test the movie the output will be “2,3,4”. As the first element of the array is 0, so the value of array1[1] is 2. The value of array1[4] is 5. But it is not sliced.

sort[edit]

Array.sort( );

The method “sort” sorts the array.

1.   var myarray = new Array("c", "a", "d", "b", "e");
2.   
3.   trace(myarray);
4.   myarray.sort();
5.   trace(myarray);

The following example creates an array, trace it then sort it and trace the sorted array. The output must be

c,a,d,b,e
a,b,c,d,e
sortOn[edit]

Array.sortOn( <fieldname> );

The method “sortOn” is similar to sort, because it sorts too. But to understand what the difference is lets test the following example:

1.   var users = new Array( { name: "U2", phone: "P3", address: "A2"},
2.                          { name: "U3", phone: "P2", address: "A1"},
3.                          { name: "U1", phone: "P1", address: "A3"} );
4.   trace("Name: " + users[0]["name"]);
5.   trace("Phone: " + users[0]["phone"]);
6.   trace("Address: " + users[0]["address"]);
7.   trace("Name: " + users[1]["name"]);
8.   trace("Phone: " + users[1]["phone"]);
9.   trace("Address: " + users[1]["address"]);
10.  trace("Name: " + users[2]["name"]);
11.  trace("Phone: " + users[2]["phone"]);
12.  trace("Address: " + users[2]["address"]);
13.  
14.  users.sortOn(“name”);
15.  
16.  trace(“------------------------------------“);
17.  trace("Name: " + users[0]["name"]);
18.  trace("Phone: " + users[0]["phone"]);
19.  trace("Address: " + users[0]["address"]);
20.  trace("Name: " + users[1]["name"]);
21.  trace("Phone: " + users[1]["phone"]);
22.  trace("Address: " + users[1]["address"]);
23.  trace("Name: " + users[2]["name"]);
24.  trace("Phone: " + users[2]["phone"]);
25.  trace("Address: " + users[2]["address"]);

You will notice that the array is ordered by “name” field, not “phone” or “address”.

splice[edit]

Array.splice( <start>, <delete count>, <value0>, <value1>, <…> );

This is almost the most powerful method of array. It does two functions: it can delete the elements of the array specified by you or it can add elements from the location also specified by you. The example below demonstrates “splice” method.

1.   var myarray = new Array( "a", "b", "c", "d");
2.   
3.   trace("--------MYARRAY--------");
4.   trace(myarray);
5.   trace("--------Adding 'c' and 'd' from location 2--------");
6.   myarray.splice(2,0,"c","d");
7.   trace(myarray);
8.   trace("--------Removing two elements from location 3 --------");
9.   myarray.splice(3,2);
10.  trace(myarray);
11.  trace("--------Removing all elements from location 1--------");
12.  myarray.splice(1);
13.  trace(myarray);

Line 1 creates an array with 4 elements: “a”, “b”, “c”, “d”. In lines 3, 5, 8 and 11 we trace information about process. In line 4 we trace our array. In line 6 we add two elements from third element. This is the use of splice if you want to add elements:

Array.splice( <start>, 0, <value0>, <value1>, <value2> );

As we don’t want to delete anything this is why the second parameter is set to 0. In line 9 we delete two elements from the forth element. This is the use of splice if you want to delete elements from specified locations:

Array.splice( <start>, <delete count>);

In line 12 we delete all elements beginning from the second element. This is the use of deleting all elements after specified location:

Array.splice( <start> );

The output of our example must be:

--------MYARRAY--------
a,b,e,f
--------Adding 'c' and 'd' from location 2--------
a,b,c,d,e,f
--------Removing two elements from location 3--------
a,b,c,f
--------Removing all elements from location 1--------
a
toString[edit]

Array.toString( );

The method “toString” converts the array to a string.

1.   var myarray = new Array(1, 2, 3, 4, 5);
2.   
3.   trace(myarray.toString());

The output is:

1,2,3,4,5
unshift[edit]

Array.unshift( <value0>, <value1>, <…> );

The method “unshift” is similar to method “push”. The only difference is that “push” adds new elements to the end of the array when “unshift” adds the elements to the beginning of the array.

1.   var myarray = new Array(3, 4);
2.   var newlength;
3.   
4.   newlength = myarray.unshift(1, 2);
5.   trace(newlength);
6.   trace(myarray);

The output is:

4
1,2,3,4


« Previous    Next »