如何匹配每個包含「tags」數組匹配值的文章對象,例如,如果我只想列出包含「foo」和「bar」關鍵字的文章?JSON:過濾數組中的匹配值
[
{
"articles": [
{
"_id": "0",
"body": "This is a test article 1.",
"tags": [ "foo", "bar" ]
},
{
"_id": "2",
"body": "This is a test article 2.",
"tags": [ "baz", "bam" ]
},
{
"_id": "4",
"body": "This is a test article 4.",
"tags": [ "foo", "bar", "baz", "bam" ]
}
]
}
]
這裏是我的快速應用程序中的路由器被調用這個JSON文件:
router.get('/:tag', function (req, res, next) {
fsAsync(function(err, data) {
var articles = data[0].articles.reverse();
var q = articles.filter(function (article) {
return article.tags === req.params.tag;
});
var json = [{ articles: q }];
// I want the results here
console.log(json);
res.render('blog-tag', {
layout: 'main',
data: json
});
});
});
謝謝!這是一種魅力! – Lanti