Solved

What is wrong with my javascript?

Posted on 2011-09-29
10
207 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
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!

 
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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Avoid defining the variables in the global scope; trying to define them in a local function scope. Because:   • Look-up is performed every time a variable is accessed.   • Variables are resolved backwards from most specific to least specific scope…
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.
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…

752 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