简体   繁体   English

如何使用lodash创建嵌套过滤器

[英]How to create a nested filter with lodash

I have the following nested JSON data structure. 我有以下嵌套的JSON数据结构。 Each node can have any number of children and the data can be any number of nodes deep. 每个节点可以具有任意数量的子代,数据可以是任意数量的深度节点。

[{
    id : "a",
    path : "a"
}, {
    id : "b",
    path : "b"
}, {
    id : "c",
    path : "c",
    children: [{
        id : "a",
        path : "c/a"
    }, {
        id : "b",
        path : "c/b",
        children: [{
            id : "a",
            path : "c/b/a"
        }, {
            id : "b",
            path : "c/b/b"
        }]
    }]
}]

I need to create a function in lodash (v3.10.1) which returns a nested JSON object of matching paths, and any parent objects. 我需要在lodash(v3.10.1)中创建一个函数,该函数返回匹配路径和任何父对象的嵌套JSON对象。 For example, if I was to search on "b" the filter should return the following: 例如,如果我要搜索“ b”,则过滤器应返回以下内容:

[{
    id : "b",
    path : "b"
}, {
    id : "c",
    path : "c",
    children: [{
        id : "b",
        path : "c/b",
        children: [{
            id : "a",
            path : "c/b/a"
        }, {
            id : "b",
            path : "c/b/b"
        }]
    }]
}]

My initial attempt was like this but it did work: 我最初的尝试是这样的,但确实奏效了:

const filterTree = (filter, list) => {
    return _.filter(list, (item) => {
        if (item.path) {
            return _.includes(item.path.toLowerCase(), filter.toLowerCase());
        } else if (item.children) {
            return !_.isEmpty(filterTree(filter, item.children));
        }
    });
};

Any help would be much appreciated 任何帮助将非常感激

The first issue is that if (item.path) is always true , so the recursive calls never happen. 第一个问题是, if (item.path)始终为true ,则递归调用永远不会发生。

In order to get your desired result, you will have to update item.children after filtering in the recursive cases, because _.filter will not mutate the array that you pass to it. 为了获得所需的结果,在递归情况下进行过滤后,必须更新item.children ,因为_.filter不会使传递给它的数组发生突变。 If you don't want the input to be mutated, use _.cloneDeep to make a copy first. 如果您不想更改输入, _.cloneDeep使用_.cloneDeep进行复制。

 const data = [{"id":"a","path":"a"},{"id":"b","path":"b"},{"id":"c","path":"c","children":[{"id":"a","path":"c/a"},{"id":"b","path":"c/b","children":[{"id":"a","path":"c/b/a"},{"id":"b","path":"c/b/b"}]}]}]; const filterTree = (filter, list) => { return _.filter(list, (item) => { if (_.includes(_.toLower(item.path), _.toLower(filter))) { return true; } else if (item.children) { item.children = filterTree(filter, item.children); return !_.isEmpty(item.children); } }); }; console.log(filterTree('b', data)); 
 .as-console-wrapper { max-height: 100% !important; } 
 <script src="https://cdn.jsdelivr.net/lodash/4.17.4/lodash.min.js"></script> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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