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
Rich Bradshaw
73.3k46 gold badges190 silver badges241 bronze badges
1 Answer 1
You can use square bracket notation, like this:
var title = pageContent[newpage][0]['title'];
answered Jan 22, 2011 at 14:59
karim79
343k67 gold badges420 silver badges409 bronze badges
Sign up to request clarification or add additional context in comments.
Comments
lang-js