Adding values
Very often when using arrays you will get the use of adding more parts inside an array. You have different ways of doing this to be sure you add the value on right place in the array. Yougot differnet commands for choosing where to put the value, the most used one is the push() command, this will add a value in the end of the array. So if you write this to our month array above:
threeMonths.push("april");;
this will make our array have 4 different parts. You can also choose to add a value in the beginning of the array, then you will use the command unshift(). You use the command like this:
threeMonths.unshift("june");;
This command will make all the currently values uniqe numbers will increase by one, so the new value will take the first place in the array and steal the first uniqe number of the array (0) from the one before we made this. So now this (june) will be number one in the array and december,october will be number 2. You can also add values in the middle of an array. In this example i want to make a new array first then put a new value in the middle so first i will show you how the splice() commnd work, the comman dwe’re gonna use to put a new value in the middle of an array then i will show you an example.
arrayName.splice(insertion point, delete value, new value);;
if you dont wanna delete or replace a value in the array just leave the delete value blank by type a 0 there. So if you see at this example you will understand it better.
var myArray:Array = new Array(1, 2, 3, 4, 5);
myArray.splice(2, 0, 6);
trace(myArray);;
If you test it the output window will display [1,2,6,3,4,5]. The splice command can be used for both removing and adding values, read more about removing values below.
You can also use a command called concat() to put two different arrays together to one.if you got two diffent arrays you can put them together like the example below. But you will have to make a new array that contains both of the array values.
var numberArray:Array = new Array(1, 2, 3);
var alphaArray:Array = new Array("a", "b", "c");
var concArray:Array = numberArray.concat(alphaArray)
trace(concArray);;
This script will return [1, 2, 3, "a", "b", "c"].
Removing values
Often when using arrays, you need to remove some of the parts inside the array. There are several ways to do this. You can choose which of the values in the array you wanna delete, to remove the first part or value in an array you must use the command shift() So here’s an example:
var numberArray:Array = new Array(1, 2, 3);
numberArray.shift();
trace (numberArray);;
This will display [2,3] in the output window. There is other methods to do it on too, you can remove the last value in the array by using the function pop(). So here’s an other example below.
var numberArray:Array = new Array(1, 2, 3);
numberArray.pop();
trace (numberArray);;
this will display [1,2] in the output window. Now we only need a command to remove a value in the middle of the array. Then we will use the same function as the one we used in the ’Adding Values’ chapter. We’re gonna use the splice() function. But this time we’re gonna leave the place where we should add a number blank. So here’s hows it works now:
arrayName.splice(insertion point, delete value);;
So this work the same as last time exept we dont add a new value. So here’s an example on how it works:
var numberArray:Array = new Array(1, 2, 3);
numberArray.splice(1, 1);
trace (numberArray);;
This will deisplay [1,3] in the output window. So thta’s what we have about removing values.
Manipulating arrays
Sometimes we dont only want to change a value, but we wanna change the whole array. There is alot of different functions to do that. We can use the function reverse() to reverse the order of the values in the array. Like this:
var numberArray:Array = new Array(1, 2, 3);
numberArray.reverse();
trace (numberArray);;
This will display [3,2,1] in the output window. Another function is the function sort() to make the order in alphabetic or numeric order. So an example:
var numberArray:Array = new Array(4, 2, 3);
numberArray.sort();
trace (numberArray);;
This will display [2,3,4] in the output window. There is one more sortings function called sortOn(). This function will sort out an array in alphabetic or numeric order, but you can choose what type of value in the array you want to put in order. So if you got an array with alot of different values, you can choose to only sort that value. You can also use the function join() to make all the values in the array to stings values, and you can also change a sepearator between the values, here’s an example:
var numberArray:Array = new Array('a', "b", "c", "o");
trace(numberArray.join(" - "));
This will display [a - b - c – o] in the output window. So thats what i got about manipulating arrays, hope you understand it.
So yes, thats all i got about arrays, if there’s something you are wondering about, just ask.