[英]lodash - how to _.filter the childs and return all parents from a json file?
I have created route.js file. 我已经创建了route.js文件。 Based on queryString it has to filter the Json object.
基于queryString,它必须过滤Json对象。 When I used _.filter method it returns entire object as response.
当我使用_.filter方法时,它返回整个对象作为响应。 Actually i want filter that productlist node and include the remaining nodes as response Please Help me .. Thanks in Advance...
实际上,我想过滤该productlist节点并包括其余节点作为响应。请帮助我..在此先感谢...
Here is the code.. 这是代码。
The JSON File 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 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){
//
});
The problem here is that the filterByProduct
parameter coll
isn't bound to the productList
array but the top-level object that contains the productList
array as well as the other nodes. 这里的问题是
filterByProduct
参数coll
没有绑定到productList
数组,而是包含productList
数组以及其他节点的顶级对象。 So you should be targeting coll.productList
instead. 因此,您应该改为针对
coll.productList
。
Also, using _.filter
(on coll.productList
) as you originally mentioned is a better idea than using _.forEach
because it iterates through the array and filters the items. 另外,如您最初提到的那样,使用
_.filter
(在coll.productList
)比使用_.forEach
更好的主意,因为它会遍历数组并过滤项目。 Try this version of filterByProduct
instead: 请尝试使用此版本的
filterByProduct
:
var filterByProduct = function(coll, productType){
return _.filter(coll.productList, function(o) {
return o.productType === productType;
})
};
Finally, to return an object similar to your JSON data file that has the filtered version of productList
nodes plus the other top-level nodes, you could use the _.clone
method to shallow clone your jsonFile
object and then overwrite the productList
and FilteredProducts
properties with the values returned from your filterByProduct
function and the length of the results of filterByProduct
, respectively. 最后,要返回类似于JSON数据文件的对象,该对象具有
productList
节点和其他顶级节点的过滤版本, productList
可以使用_.clone
方法来浅克隆jsonFile
对象,然后覆盖productList
和FilteredProducts
属性分别使用从filterByProduct
函数返回的值和filterByProduct
结果的长度。 Here's what I came up with: 这是我想出的:
if (req.query.productType) {
var stringObj = _.clone(jsonFile);
stringObj.productList = filterByProduct(jsonFile, req.query.productType);
stringObj.FilteredProducts = stringObj.productList.length;
res.end(stringObj);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.