By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,963 Members | 2,016 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,963 IT Pros & Developers. It's quick & easy.

converting a string to an object

P: n/a
Here is my situation:

I have several arrays ... let's say ... Bob1_1, Bob1_2, etc.
Each array has several elements ... element 1 is the one I am
interested in.

Example:
Bob1_1=new Array("Element 0","NewWin=window.open('thispage.html')");
Bob1_2=new Array("Element 0","NewWin=window.open('thatpage.html')");

I am tracking which array is currently being used through a variable
.... say, 'CurrentArray'.

I want to do something like this:

eval ('Bob'+CurrentArray+'[1]') but the statement does nothing. Upon
checking the type using typeof it reports back a string which makes
sense.

Doing this works fine: eval(Bob1_2[1]) but I need to build that
statement dynamically.

Any ideas ????
Jul 20 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
Lee
D Elkins said:

Here is my situation:

I have several arrays ... let's say ... Bob1_1, Bob1_2, etc.
Each array has several elements ... element 1 is the one I am
interested in.

Example:
Bob1_1=new Array("Element 0","NewWin=window.open('thispage.html')");
Bob1_2=new Array("Element 0","NewWin=window.open('thatpage.html')");

I am tracking which array is currently being used through a variable
... say, 'CurrentArray'.

I want to do something like this:

eval ('Bob'+CurrentArray+'[1]') but the statement does nothing. Upon
checking the type using typeof it reports back a string which makes
sense.

Doing this works fine: eval(Bob1_2[1]) but I need to build that
statement dynamically.

Any ideas ????


The first idea that comes to mind is that any time you find yourself
using eval(), you've probably overlooked a simpler solution.

The second idea that comes to mind is that maybe CurrentArray
doesn't have the value you expect it to have.

You could eliminate the eval() by using array notation, as in:

window["Bob"+CurrentArray][1]

but that's sort of ugly.
Another solution would be:

Bob=new Object()
Bob["1_1"]=new Array("Element 0","NewWin=window.open('thispage.html')");
Bob["1_2"]=new Array("Element 0","NewWin=window.open('thatpage.html')");

Accessed as:
Bob[CurrentArray][1]

And another would appear to be to use two more levels of array:

Bob=new Array();
Bob[1]=new Array();
Bob[1][1]=new Array("Element 0","NewWin=window.open('thispage.html')");
Bob[1][2]=new Array("Element 0","NewWin=window.open('thatpage.html')");

but this means you have to track both indices of the current array:

Bob[CurrentArray.major][CurrentArray.minor][1]

Jul 20 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.