converting a string to an object

Discussion in 'Javascript' started by D Elkins, Oct 14, 2003.

  1. D Elkins

    D Elkins Guest

    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 ????
    D Elkins, Oct 14, 2003
    #1
    1. Advertising

  2. D Elkins

    Lee Guest

    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]
    Lee, Oct 14, 2003
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Wouter vanEck

    Converting html string to object

    Wouter vanEck, Jan 7, 2005, in forum: ASP .Net
    Replies:
    10
    Views:
    737
    Wouter vanEck
    Jan 10, 2005
  2. Prakash Prabhu
    Replies:
    2
    Views:
    1,118
    Roedy Green
    Sep 2, 2003
  3. deepak
    Replies:
    2
    Views:
    1,612
    deepak
    Feb 14, 2008
  4. KevinSimonson
    Replies:
    6
    Views:
    618
  5. Erinys
    Replies:
    1
    Views:
    95
    Grant Wagner
    Nov 16, 2004
Loading...

Share This Page