Page 1 of 1

dynamic arrays in javascript [solved]

Posted: Wed Apr 05, 2006 12:37 pm
by J_Iceman05
I the searches I have done, the only thing i have found on this is either static arrays, or things like using dynamic select boxes with javascript...

I am trying to be able to create an array in javascript, but I have no idea what the names or values for variables might be until run time
Example: in PHP
user submits text boxes with the following information
name[0] = one, value[0] = 1
name[1] = two, value[1] = 2
name[2] = first, value[2] = jack
name[3] = last, value[3] = smith
I can loop through and say something like...

Code: Select all

for ($num=0; $num < count($name); $num++){
        $newArray[$name[$num]] = $value[$num];
}
print_r($newArray);
with the output of
Array([one] => 1, [two] => 2, [first] => jack, [last] => smith);

I have tried this same basic thing in javascript
I described the variable that will be the array:
var VariableArray;

and when creating that array I have tried
VariableArray[name] = value;
But the entire javascript stops working as soon as it reaches this point in the loop.

Commenting that one line will allow the javascript to finish running and everything but that one line seems to be working.
Is there something wrong with the way I am trying to set the array, or is it just that you can't do that at all?

Extra Info:
I don't want to need to refresh the page, or actually submit to any page, hence the desire for using javascript and not PHP
I also don't know how many variables there will be. I am not actually taking the values from input boxes labeled "name[]" or "value[]".
The info for what the name and value will be is actually coming from a single string, so to find the names and values wouldn't actually be name[0]=one, value[0]=1; it would read more like "&one=1&two=2&first=jack&last=smith" (i think ampersands are good to use as separaters)
I have the loop searching through the string to find the name and value pairs (this is working correctly)
The only problem seems to be just creating the array

Thank you in advanced.
Everyone's help will be greatly appreciated.

Posted: Wed Apr 05, 2006 12:43 pm
by feyd

Code: Select all

var VariableArray = new Array();

Posted: Wed Apr 05, 2006 12:52 pm
by J_Iceman05
:oops: see.. now I feel like an idiot.
I have tried

Code: Select all

var VariableArray = array();
but not

Code: Select all

var VariableArray = new Array();
Thank you very much feyd