Adding to an array

Is there a way to add a value to a preexisting array? I am by no means an actionscript expert so if you know could you try to explain it as simply as possible? Or is this function already simple and I just havent found it?

Many thanks to any who can help.

It is called push()

Try this…

myArray = ["a", "b"];
myArray.push("c");
trace(myArray);

The original array only has “a” and “b”, but the push adds “c”, so the output in the window should be “a,b,c” (no quotes)

Push adds the value at the end of the array. Unshift adds the value at the begining of the array:


myArray1 = ["1", "2"];
myArray2 = ["1", "2"];
myArray1.unshift ("3");
myArray2.push ("3");
trace ("myArray1 = " + myArray1);
trace ("myArray2 = " + myArray2);


//Outputs:
// myArray1 = 3,1,2
// myArray2 = 1,2,3

just FYI :slight_smile:

Cheers,
jubs :rambo:

Ah, didn’t think about adding to the FRONT of the array… good catch there Jubba :slight_smile:

thank you