Solved

What is wrong with my javascript?

Posted on 2011-09-29
10
204 Views
Last Modified: 2012-06-21
Hello Experts,

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

Open in new window


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!


0
Comment
Question by:jagku
  • 4
  • 3
  • 2
  • +1
10 Comments
 
LVL 82

Accepted Solution

by:
leakim971 earned 450 total points
ID: 36815176
Same idea :


var value[] = 1;
value[] = 2;

var my[0] = 123;
var my[1] = 4;

try{
   eval("myArray_"+my[0]+"_"+my[1]);
}
catch(e) {
   eval("var myArray_"+my[0]+"_"+my[1]+ " = new Array();");
}
eval("myArray_"+my[0]+"_"+my[1]+"[] = " + value + ";" ); // eg myArray_123_4
0
 
LVL 82

Assisted Solution

by:leakim971
leakim971 earned 450 total points
ID: 36815198
or perhaps :

var value[] = 1;
value[] = 2;

var my[0] = 123;
var my[1] = 4;

if(window["myArray_"+my[0]+"_"+my[1]] === undefined)
{
      window["myArray_"+my[0]+"_"+my[1]] = new Array();
}
window["myArray_"+my[0]+"_"+my[1]] = value; // eg myArray_123_4
0
 
LVL 6

Assisted Solution

by:Silhouette
Silhouette earned 50 total points
ID: 36815221
You can use:

if(typeof("myArray_"+my[0]+"_"+my[1]) === undefined)
0
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 
LVL 63

Expert Comment

by:Zvonko
ID: 36815270
Check this:
<script>
var myvalue = [];
myvalue[0] = 2;


var my = [];
my[0] = 123;
my[1] = 4;

if(window["myArray_"+my[0]+"_"+my[1]] == undefined){
   alert("Undefinded.");
   window["var myArray_"+my[0]+"_"+my[1]] = new Array();
}
window["myArray_"+my[0]+"_"+my[1]] = myvalue; // eg: myvalue

alert(window["myArray_"+my[0]+"_"+my[1]]);
</script>

Open in new window

0
 
LVL 63

Expert Comment

by:Zvonko
ID: 36815280
Sorry...
0
 

Assisted Solution

by:jagku
jagku earned 0 total points
ID: 36815520
Hi leakim971 and Silhouette, thanks for this.

Leakim971: I get a syntax error on this line:

eval("myArray_"+my[0]+"_"+my[1]+"[] = " + value + ";" ); // eg myArray_123_4

Any idea why this is?
0
 
LVL 82

Expert Comment

by:leakim971
ID: 36815625
value is 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

Open in new window

0
 

Author Comment

by:jagku
ID: 36815687
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

0
 

Author Comment

by:jagku
ID: 36816225
Actually, I think I need:

eval("myArray_"+my[0]+"_"+my[1]+"push(value);" );





0
 

Author Closing Comment

by:jagku
ID: 36908433
Actually, it should be:

eval("myArray_"+my[0]+"_"+my[1]+".push(value);" );
0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Explain how this javascript function works 1 47
Browser Chrome downloads colored link 5 41
PHP Form Calculate Total Price 10 42
Button and js nou working 3 8
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question