请给我一些帮助,我的节点/表达知识非常有限,但是我只是想做一些非常基本的事情,如果是C#,我不会有任何问题。
假设以下json数据结构,如何使用node.js显示“ currentstage” =“ Beta”的项目计数,并使用express.js将值输出到模板?无论我尝试什么,都不会得到任何回报,甚至只是一个纯字符串。
Json:
{
"items":[
{
"currentstage": "Beta",
"title": "This will be the title",
"body": "This will be the body text",
"externallink": "http://example.com",
"contact": "John Does",
"stages":[
{
"Alpha":[{
"description": "Started alpha",
"start": "2018-01-01"
}]
},
{
"Beta":[{
"description": "Started beta",
"start": "2018-04-01"
}]
}
]
}
{
"currentstage": "Alpha",
"title": "This will be the title",
"body": "This will be the body text",
"externallink": "http://example.com",
"contact": "John Does",
"stages":[
{
"Alpha":[{
"description": "Started alpha",
"start": "2018-03-01"
}]
}
]
}
]
}
Route.js json文件与routes.js文件位于同一文件夹中
exports.index = function (req, res) {
var obj = require('projectdata.json');
res.render('index', { items: obj.filter(function(value) { return value.currentstage === "Beta" }).length } );
};
index.html
<p>Items at Beta: {{items}}</p>
答案 0 :(得分:1)
您应该遍历obj.items
数组而不是包含项数组的对象obj
:
obj.items.filter(function(value) { return value.currentstage === "Beta" }).length