2

I have an object (that I can change!) that looks like this:

var pageContent = {
 "contact_us":[{
 "content":"<p>Lots of content here.</p>",
 "title":"Contact Us"
 }],
 "index":[{
 "content":"<p>Some other content here.</p>",
 "title":"Home Page"
 }],
 "gallery":[{
 "content":"<p>Some more content</p>",
 "title":"Gallery"
 }],
 }

To access properties, I'm doing this:

pageContent.gallery[0]['title'];

To get the value "Gallery".

What I want to do is something like this:

var newPage = "gallery";
pageContent.newpage[0]['title'];

But, obviously that fails. How can I do what I want? Changes to anything are welcome, nothing is set in stone here.

asked Jan 22, 2011 at 14:56

1 Answer 1

4

You can use square bracket notation, like this:

var title = pageContent[newpage][0]['title'];
answered Jan 22, 2011 at 14:59
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.