Nehross Montoya
Nehross Montoya

Reputation: 27

Array attribute of an object

In a non-array attribute of an object:

function myObj()
{
  this.code = "code";
  this.name = "name";
}

I can retrieve their values using:

  myCode = myObj.code;

or:

  myCode = myObj["code"];

With arrays:

function myObj()
{
  this.code = ["code1","code2"];
  this.name = ["name1","name2"];
}

I have to to get "code2" so my syntax should be:

  myCode = myObj.code[1];

Now my problem is, how can I get "code2" using the other way (the one the uses braces and the attribute enclosed in braces)?

Upvotes: 0

Views: 146

Answers (1)

Niet the Dark Absol
Niet the Dark Absol

Reputation: 324750

You mean like this?

myCode = myObj['code'][1];

Upvotes: 3

Related Questions