Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

What is wrong with my javascript?

Posted on 2011-09-29
10
Medium Priority
?
211 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 1800 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 1800 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 200 total points
ID: 36815221
You can use:

if(typeof("myArray_"+my[0]+"_"+my[1]) === undefined)
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

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

Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
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

916 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