PDA

View Full Version : Adding to an array



Big_Al
February 22nd, 2003, 05:39 PM
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.

lostinbeta
February 22nd, 2003, 06:01 PM
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)

Jubba
February 22nd, 2003, 06:26 PM
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 :)

Cheers,
jubs :rambo:

lostinbeta
February 22nd, 2003, 06:30 PM
Ah, didn't think about adding to the FRONT of the array.... good catch there Jubba :)

Big_Al
February 23rd, 2003, 03:31 PM
thank you