# ActionScript Programming/PartI/Chapter 3/Arrays/Methods
## Data types
### Arrays
#### Methods

| 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

Array.concat( <array1>, <array2> );
The method “concat” combines arrays with each other. For example this code combines two arrays.
The result will be:

##### join

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
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

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.
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:

##### push

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.
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:

##### reverse

Array.reverse( );
“reverse” method reverses the array.
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:

##### shift

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.
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:

##### slice

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.
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

Array.sort( );
The method “sort” sorts the array.
The following example creates an array, trace it then sort it and trace the sorted array. The output must be

##### sortOn

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:
You will notice that the array is ordered by “name” field, not “phone” or “address”.

##### splice

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.
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:
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:
In line 12 we delete all elements beginning from the second element. This is the use of deleting all elements after specified location:
The output of our example must be:

##### toString

Array.toString( );
The method “toString” converts the array to a string.
The output is:

##### unshift

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.
The output is:
« Previous   Next »