Reputation: 724
I am trying to dynamically load content from this object
projects = {
test1:{
'name': 'Test',
'description': 'This is just a test description for a test project.',
'img': {
'thumbnail': '/img/260x180.png',
'screenshot': '/img/1024x500.png'
},
'link': '/projects/test1'
},
test2:{
'name': 'Test2',
'description': 'This is just a test description for a test project.',
'img': {
'thumbnail': '/img/260x180.png',
'screenshot': '/img/1024x500.png'
},
'link': '/projects/test2'
}
};
as follows
var id = req.params.id;
res.send('index/project',{title: title, project: projects.id});
and it is coming back as undefined. I also tried use JSON.stringify() and that didn't work either. Any ideas?
Upvotes: 0
Views: 83
Reputation: 1074108
I think you might be looking for bracketed notation:
res.send('index/project',{title: title, project: projects[id]});
// Change here ------------------------------------------^^^^
In JavaScript, you can access properties of an object either using dot notation and a literal property name (obj.foo
), or bracketed notation and a string property name (obj["foo"]
). In the latter case, of course, the string can be the result of any expression, including a variable reference. So assuming req.params.id
is a string like "test1"
, "test2"
, and such, since you're putting that in id
you'd use projects[id]
to refer to the test1
or test2
properties of projects
.
Upvotes: 4