Solved

What is wrong with my javascript?

Posted on 2011-09-29
10
202 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
 
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
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 

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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Having worked on larger scale sites, we found out that you are bound to look at more scalable solutions to integrating widgets, code snippets or complete applications and mesh them into functional sites, in any given composition. To share some of…
JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
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…

744 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now