PDA

View Full Version : Arrays



InNeedofHelp
02-27-2006, 08:33 PM
Hey DD,

I got a question I've been curious to know the answer to for several months, but have never looked into finding that answer. :p

With arrays, is there a way to add a variable to an already existing array?
For example:

var fruits=new Array();
fruits[0]="apples"
fruits[1]="bananas"
fruits[2]="oranges"


Is it possible to then go and using a function add a fruits[3]="pears" to that already existing array?

jscheuer1
02-27-2006, 08:44 PM
You can do that anytime after the array is created by simply doing this:

fruits[3]="pears"

Twey
02-27-2006, 09:05 PM
Or, fruits.push("pears")

InNeedofHelp
02-27-2006, 09:07 PM
Wow....really?
For some reason I have a vague memory of seeing some javascript function that added to an array. wow....I'm shocked :p I feel stupid hahahah.
Is that all that's needed though? Do you have to have it like this?


var fruits=new Array();
fruits[0]="apples"
fruits[1]="oranges"
fruits[2]="bananas"
fruits[3]=""
fruits[4]=""


In order to add to the array? or does it only have to go as high up as i have variables for, then i can add to it with fruits[3]="pears" whenever?

jscheuer1
02-27-2006, 09:24 PM
You can do this:


var fruits=new Array();
fruits[3]="pears"


Then later do this:



fruits[0]="apples"
fruits[1]="oranges"
fruits[2]="bananas"

Arrays are very flexible.

If after you've done the above, you want to insert a new number two and have all the other ones (except 0 and 1) get bumped up a number, that's when things get a little tricky.

InNeedofHelp
02-27-2006, 09:40 PM
Allright, great. :D

Thanks.