【发布时间】:2017-01-18 06:44:56
【问题描述】:
我已经创建了 route.js 文件。基于 queryString 它必须过滤 Json 对象。当我使用 _.filter 方法时,它会返回整个对象作为响应。实际上我想过滤该产品列表节点并将其余节点包括在内作为响应 请帮助我..在此先感谢...
这是代码..
JSON 文件
{
"productList": [
{
"productName": "xyz",
"productType": "mobile"
},
{
"productName": "xyz",
"productType": "mobile"
},
{
"productName": "xyz",
"productType": "mobile"
}
],
"totalProducts": 3,
"FilteredProducts": 0,
"test1": 11,
"test11": 12,
"test33": 13
}
route.js
var filterByProduct = function(coll, productType){
return _.forEach(coll, function(o){
return _.find(o, function(item){
});
});
};
var queryString = function(req, res, next) {
if (req.query.productType ) {
var stringObj = JSON.stringify(filterByProduct(jsonFile, req.query.productType),null,4);
res.end(stringObj);
} else if (req.query !== {}) {
var stringObj = JSON.stringify(jsonFile,null,4);
res.end(stringObj);
} else {
res.end('Not a Query String');
}
}
router.get('/test', queryString, function(req,res){
//
});
【问题讨论】:
标签: javascript json node.js express lodash