Using a variable as part of a javascript array
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 mo开发者_JAVA技巧re 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.
You can use square bracket notation, like this:
var title = pageContent[newpage][0]['title'];
精彩评论