简体   繁体   English

lodash-如何_.filter孩子并从json文件返回所有父母?

[英]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对象,然后覆盖productListFilteredProducts属性分别使用从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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM