0

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)?

1
  • Javascript objects have properties, HTML and XML elements have attributes. The term you are looking for is "square bracket notation". Commented May 16, 2012 at 6:57

1 Answer 1

3

You mean like this?

myCode = myObj['code'][1];
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for the quick response..^^, I have tried it before but didn't work. But when I tried it now it did work. Must have missed something before. Anyways, thank you very much. ^^,

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.