Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Wanet Telecoms Ltd on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Problems with eval and arrays

Status
Not open for further replies.

ALSav

Technical User
Feb 21, 2001
66
GB
Hi,

I am trying to create a simple hierarchical menu with data for menus stored in arrays. It looks like this with backgroundArray[8] linking to a submenu populated with values from child1Array. backgroundArray[8].child is a string at at some point in my createMenu() I need to convert the string "child1" back into an array. To do this I have used

var subMenuArray = eval(backgroundArray[8].child + "Array")

I copied this from a similar menu in a book and it worked fine in their example but it doesn't for me.
Is this an apprpriate use of eval?

Ideally I would like to store backgroundArray[8].child as an array and not a string but I'm doing something wrong there too. Should it be possible? How would I change the arrayValues() function?

all ideas welcome

thanks
ALS

function arrayValues(text,child,image,url,ID)
{
this.text = text;
this.child = child;
this.image = image;
this.url = url;
this.ID = ID; //menu ID
}


function menuSetUp()
{
backgroundArray = new Array();
backgroundArray[1] =new arrayValues("Background",null,"minisun","page1.htm","menu1");
backgroundArray[2] =new arrayValues("Sustainability",null,null,"backgroundSustainable.htm","menu1");
backgroundArray[3] =new arrayValues("Environment",null,null,"backgroundEnvironment.htm","menu1" );
backgroundArray[4] =new arrayValues("Pollution",null,null,"backgroundPollution.htm","menu1");
backgroundArray[5] =new arrayValues("Recycling",null,null,"backgroundRecycling.htm","menu1");
backgroundArray[6] =new arrayValues("Energy",null,null,"backgroundEnergy.htm","menu1");
backgroundArray[7] =new arrayValues("Water",null,null,"backgroundWater.htm","menu1");
backgroundArray[8] =new arrayValues("Biodiversity","child1",null,null,"menu1");

createMenu(backgroundArray)

child1Array = new Array();

child1Array[1] = new arrayValues("more biodiversity 1",null,null,"subBio1.htm","menu7");
child1Array[2] = new arrayValues("more biodiversity 2",null,null,"subBio2.htm","menu7");
child1Array[3] = new arrayValues("more biodiversity 3",null,null,"subBio3.htm","menu7");
child1Array[4] = new arrayValues("more biodiversity 4",null,null,"subBio4.htm","menu7");
child1Array[5] = new arrayValues("more biodiversity 5",null,null,"subBio5.htm","menu7");
child1Array[6] = new arrayValues("more biodiversity 6",null,null,"subBio6.htm","menu7");
child1Array[7] = new arrayValues("more biodiversity 7",null,null,"subBio7.htm","menu7");

 
If you create the child1Array before the backgroundArray, then you can pass child1Array as the child parameter when you generate the backgroundArray.

Like this:
Code:
<script>

function arrayValues(text,child,image,url,ID) 
{ 
    this.text = text;
    this.child = child;
    this.image = image;
    this.url = url;
    this.ID = ID; //menu ID
}


    child1Array = new Array();
    child1Array[1] = new arrayValues(&quot;more biodiversity 1&quot;,null,null,&quot;subBio1.htm&quot;,&quot;menu7&quot;);
    child1Array[2] = new arrayValues(&quot;more biodiversity 2&quot;,null,null,&quot;subBio2.htm&quot;,&quot;menu7&quot;);
    child1Array[3] = new arrayValues(&quot;more biodiversity 3&quot;,null,null,&quot;subBio3.htm&quot;,&quot;menu7&quot;);
    child1Array[4] = new arrayValues(&quot;more biodiversity 4&quot;,null,null,&quot;subBio4.htm&quot;,&quot;menu7&quot;);
    child1Array[5] = new arrayValues(&quot;more biodiversity 5&quot;,null,null,&quot;subBio5.htm&quot;,&quot;menu7&quot;);
    child1Array[6] = new arrayValues(&quot;more biodiversity 6&quot;,null,null,&quot;subBio6.htm&quot;,&quot;menu7&quot;);
    child1Array[7] = new arrayValues(&quot;more biodiversity 7&quot;,null,null,&quot;subBio7.htm&quot;,&quot;menu7&quot;);


    backgroundArray = new Array();
    backgroundArray[8] =new arrayValues(&quot;Biodiversity&quot;,child1Array,null,null,&quot;menu1&quot;);


    for( i=1; i < backgroundArray[8].child.length; i++ ) {

        alert( backgroundArray[8].child[i].url );
    }

</script>

 
Thanks for the help. It looks like I was trying to use an array before I had actually created it!

regards

ALSav
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top