converting a string to an object

D

D Elkins

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 ????
 
L

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]
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,576
Members
45,054
Latest member
LucyCarper

Latest Threads

Top