jagku
asked on
What is wrong with my javascript?
Hello Experts,
I am trying to dynamically create a variable as follows:
I get an error on the following line:
if(eval("myArray_"+my[0]+" _"+my[1]) === undefined)
Error: 'myArray_123_4' is undefined
a) What am I doing wrong?
b) Once corrected, do I access this variable by:
var x = eval('myArray_123_4'); ?
Thanks!
I am trying to dynamically create a variable as follows:
var value[] = 1;
value[] = 2;
var my[0] = 123;
var my[1] = 4;
if(eval("myArray_"+my[0]+"_"+my[1]) === undefined)
{
eval("var myArray_"+my[0]+"_"+my[1]+ " = new Array();");
}
eval("myArray_"+my[0]+"_"+my[1]+"[] = " + value + ";" ); // eg myArray_123_4
I get an error on the following line:
if(eval("myArray_"+my[0]+"
Error: 'myArray_123_4' is undefined
a) What am I doing wrong?
b) Once corrected, do I access this variable by:
var x = eval('myArray_123_4'); ?
Thanks!
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Sorry...
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
value is a number?
else you have :
myArray_123_4 = hello;
instead :
myArray_123_4 = "hello";
if it's not a number :
else you have :
myArray_123_4 = hello;
instead :
myArray_123_4 = "hello";
if it's not a number :
eval("myArray_"+my[0]+"_"+my[1]+"[] = \"" + value + "\";" ); // eg myArray_123_4
or
eval("myArray_"+my[0]+"_"+my[1]+"[] = '" + value + "';" ); // eg myArray_123_4
ASKER
Hi,
value is an array (I take in JSON array and want to assign each part of it to myArray_... ).
ie
eval("myArray_"+my[0]+"_"+ my[1]+"[] = " + value + ";" ); // eg myArray_123_4
The above string should avaluate to.
myArray_123_4[] = value;
I hope that makes sense?
Thanks
value is an array (I take in JSON array and want to assign each part of it to myArray_... ).
ie
eval("myArray_"+my[0]+"_"+
The above string should avaluate to.
myArray_123_4[] = value;
I hope that makes sense?
Thanks
ASKER
Actually, I think I need:
eval("myArray_"+my[0]+"_"+ my[1]+"pus h(value);" );
eval("myArray_"+my[0]+"_"+
ASKER
Actually, it should be:
eval("myArray_"+my[0]+"_"+ my[1]+".pu sh(value); " );
eval("myArray_"+my[0]+"_"+
Open in new window