ActionScript Programming/PartI/Chapter 3/Arrays/Methods

concat
Array.concat;

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

The result will be:

a,b,c,d,5,f

join
Array.join;

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:

4 1,2,3

push
Array.push;

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:

4 1,2,3,4

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:

1,2,3,4,5 5,4,3,2,1

shift
Array.shift;

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:

1 2,3,4

slice
Array.slice;

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

c,a,d,b,e a,b,c,d,e

sortOn
Array.sortOn;

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(, , , , <…> );

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:

Array.splice(, 0, , , );

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;

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;

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
Array.toString;

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

1,2,3,4,5

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

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:

4 1,2,3,4

« Previous &emsp;&emsp; Next »