Orb Hitter
Orb Hitter

Reputation: 317

Can you have a variable within a variable in javascript?

I need to do something like this:

Section 1, Chapter 1 title is "Dogs"

Section 1, Chapter 2 title is "Cats"

Section 2, Chapter 1 title is: "Goldfish"

I want to be able to write it something like this, with arrays:

section[0].chapter[0] = "Dogs";
section[0].chapter[1] = "Cats";
section[1].chapter[0] = "Goldfish";

Upvotes: 0

Views: 99

Answers (2)

WonderfulDay
WonderfulDay

Reputation: 148

This other post on stackoverflow has some answeres on how to create 2-dimensional arrays. How can I create a two dimensional array in JavaScript?

Or you could use a "class", though this may be needlessly complex...

function Section ()
{
  this.Chapters = new Array( 10 );
}

function CreateSection ()
{
  var sections = new Array ( 10 );

  sections[0] = new Section ();
  sections[0].Chapters[0] = "dogs";

  alert (  sections[0].Chapters[0] );
}

Upvotes: 1

Mike Sav
Mike Sav

Reputation: 15291

do you mean like a Multi Dimentional Array?

How can I create a two dimensional array in JavaScript?

Upvotes: 0

Related Questions