Solved

What is wrong with my javascript?

Posted on 2011-09-29
10
208 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

The task A number given should be formatted for easy reading by separating digits into triads. Format must be made inline via JavaScript, i.e., frameworks / functions are not welcome. So let’s take a number like this “12345678.91¿ and format i…
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…
Suggested Courses

624 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