Main Content

You are here:

Array Functions

Hello and welcome. Today we shall be learning about an extended concept of ActionScript, arrays functions. Arrays are used for storing groups of information, and you can real in detail about them on the array tutorial. I am assuming for now that you have a bit of an idea about what arrays are, and want to know more. Well, here's you're chance as we delve into the world of array functions: functions which alter arrays or return some information about arrays. Though these will work in the same way in both AS2 and AS3, this tutorial is particularly designed for AS3.

If any of you know any other programming languages, you will probably realise that these functions (with their eccentric names) work in exactly the same way - you will just need to know how to apply them, which is like this:

array_name.function_name(arguments);

What everyone needs to know, however, is that these functions not only alter the array everytime you run them, but also return a value when they do. For these reasons, I will be using this code (which you are more than welcome to try out for the various functions) as the basis for my examples:

var rainbow = ["Red", "Orange", "Yellow", "Green", "Blue", "Indigo", "Violet"];
trace("Array before function: " + rainbow);
trace("Function returns: " + rainbow.function(..));
trace("Array after function: " + rainbow);

So, on with the functions...

Shift

Does what? Removes the first element (thing) in the array. Returns it. Takes Arguments: None. Returns: The first element. Example: After rainbow.shift(), the array has no "Red" string in it and starts with "Orange".

Pop

Does what? Removes the last element in the array. Returns it. Takes Arguments: None. Returns: The last element. Example: After rainbow.pop(), the array has no "Violet" string in it and ends with "Indigo".

Unshift

Does what? Tacks on any number of elements onto the start of an array. Takes Arguments: Unlimited number of string, ints etc. Returns: The new length of the array. Example: After rainbow.unshift("X-Rays","Infra-red"), the array begin with "X-Rays", then "Infra-red", then "Red" and so on. The function returns the number '9'.

Push

Does what? Tacks on any number of elements onto the end of an array. Takes Arguments: Unlimited number of string, ints etc. Returns: The new length of the array. Example: After rainbow.push("UV","Microwave"), the last three elements in the array are "Violet", "UV" and then "Microwave". The function returns the number '9'.

Reverse

Does what? Reverse the orders of the elements in the array. Takes Arguments: None. Returns: The new array. Example: After rainbow.reverse(), the array ends up as "Violet","Indigo",...,"Orange","Red" The function also returns this updated array.

Sort

Does what? Sorts the elements in the array. Takes Arguments: All optional. Returns: The new array (default) or some special codes. Example: After rainbow.sort(), the array ends up as "Blue","Green",...,"Violet","Yellow" i.e. Alphabetical order. The function also returns this updated array. Note! Sorting is, by default: case-sensitive by default (Z precedes a); ascending (a precedes b), is done as though all elements were strings (100 precedes 99). For information on how to change this, visit .

Join

Does what? Joins the elements in the array into a long string. Takes Arguments: the delimiter: this goes in between the elements when they are joined together (optional : a comma by default). Returns: A big long string. Example: After rainbow.join(" -> "), the array is NOT CHANGED but the function has returned this string: Red -> Orange -> [...] Blue -> Indigo -> Violet

indexOf

Does what? Finds the first instance of an object/string/int etc in the array. Takes Arguments: what to look for (a string/int etc), the start index (optional: defaults to 0) Returns: the index of the first instance, or -1 if it is not found Example: After rainbow.indexOf("Orange"), the array is NOT CHANGED but the function has returned "1" (it's the second element). After rainbow.indexOf("Orange",2), the function returns "-1" (not found) because it started looking at index 2 ("Yellow") onwards.

lastIndexOf

Does what? Finds the last instance of an object/string/int etc in the array. Takes Arguments: what to look for (a string/int etc), the start index (optional: defaults to a very large number) Returns: the index of the last instance, or -1 if it is not found Example: After rainbow.lastIndexOf("Orange"), the array is NOT CHANGED but the function has returned "1" (it's the second element). After rainbow.indexOf("Orange",2), the function also returns "1" because although it started looking at index 2 ("Yellow"), it worked backwards, finding "orange".

Slice

Does what? Creates a new array based on your array. Takes Arguments: A start index (optional: defaults to 0) - the returned array includes this element and element and all elements up to, but not including the element given by: the end index (optional: defaults to a large number). If the start or end index is negative, the compiler will count backwards from the end (-1 would be the last element, -2 the penultimate etc). Returns: a new array Example: After rainbow.slice(0,2), the array is NOT CHANGED but the function has returned a new array: ["Red","Orange"]. rainbow.slice(-2) returns ["Indigo","Violet"] and rainbow.slice(1,0) returns an empty array.

Splice

Does what? Adds elements to and removes elements from an array. Takes Arguments: A start index - an integer that specifies where in the array the insertion or deletion begins. A negative number will be counted backwards from the end of the array (see slice()). The number of elements to remove (optional: defaults to 'everything until the end') 0 means none. An unlimited number of elements to insert at that point. Returns: a new array containing all the elements (if any) that were removed. Example: After rainbow.splice(6,1,"Purple"), the seventh element of the array has been changed from "Violet" to "Purple" (or rather, "Violet" has been removed and "Purple" added). The function returns a new array: ["Violet"].

So, it turns out there are actually quite a few array functions (some not even listed here) - you should be able to find one to fit your requirements! See you for the next free ActionScript tutorial...

Harry.

Comments

var ff = ["What?","great","Flash","what!","Foundation","Guess"];
trace(ff.pop() + " " + ff.splice(3,1));
trace(ff.shift());
ff.reverse().push("is",ff.splice(2,1)[0]);
trace(ff.join(" ") + "!");