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.

1 Answer 1

4

You can use square bracket notation, like this:

var title = pageContent[newpage][0]['title'];
Sign up to request clarification or add additional context in comments.

Comments

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.