converting a string to an object

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • D Elkins

    converting a string to an object

    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=wind ow.open('thispa ge.html')");
    Bob1_2=new Array("Element 0","NewWin=wind ow.open('thatpa ge.html')");

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

    I want to do something like this:

    eval ('Bob'+CurrentA rray+'[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 ????
  • Lee

    #2
    Re: converting a string to an object

    D Elkins said:[color=blue]
    >
    >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=wind ow.open('thispa ge.html')");
    >Bob1_2=new Array("Element 0","NewWin=wind ow.open('thatpa ge.html')");
    >
    >I am tracking which array is currently being used through a variable
    >... say, 'CurrentArray'.
    >
    >I want to do something like this:
    >
    >eval ('Bob'+CurrentA rray+'[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 ????[/color]

    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"+CurrentAr ray][1]

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

    Bob=new Object()
    Bob["1_1"]=new Array("Element 0","NewWin=wind ow.open('thispa ge.html')");
    Bob["1_2"]=new Array("Element 0","NewWin=wind ow.open('thatpa ge.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=wind ow.open('thispa ge.html')");
    Bob[1][2]=new Array("Element 0","NewWin=wind ow.open('thatpa ge.html')");

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

    Bob[CurrentArray.ma jor][CurrentArray.mi nor][1]

    Comment

    Working...