How can access an object value in an array? Specifically "namecase" value? I have a ejs view looping in the array so that it can display all my object values. I'm passing data through routes.
//data.json
{
"works": [{
"company" : "Company 1",
"projects": [{
"namecase":"Projectname 1",
"desc":"This is a project with fairies.",
"img" : "/images/placeholder.jpg",
"url" : "/"
},
{
"namecase":"Projectname 2",
"desc":"This is a project with monsters.",
"img" : "/images/placeholder.jpg",
"url" : "/"
}]
}
]
}
//index.js route
var appdata = require('../data.json');
router.get('/work', function(req, res) {
var myProjects = [];
appdata.works.forEach ( function (item){
//this is where I pull object from json
myProjects = myProjects.concat(item.projects["namecase"]);
});
res.render('work', {
title: 'Work',
projects: myProjects
});
});
///ejs
<% if (projects.length > 0) { %>
<% for (i=0; i<projects.length; i++) { %>
<span><%= projects["namecase"] %></span>
<% }
What you want is to concat
item.projects, not thenamecasekey as you're doing. Also, once you're in theworksobject'sprojectschild object, you'll want to loop through the projects and then concat them, like so:Doing so will return just the projects: