简体   繁体   English

A Javascript function 用搜索词过滤树结构 json。 排除与搜索词不匹配的任何 object

[英]A Javascript function to filter tree structured json with a search term. exclude any object which donot match the search term

I am havin a json with following structure which can go up to n level depth:我有一个 json 具有以下结构,它可以 go 达到 n 级深度:

[{
name: 'p1',
child:[{
    name: 'c1',
    child: [{
        name: 'gc1',
        child: []
    },{
        name: 'gc2',
    child:[]
    }]
},{
    name: 'c2',
    child: [{
        name: 'gc1',
        child: []
    },{
        name: 'gc2',
    child:[]
    }]
},{
    name: 'c3',
    child: [{
        name: 'gc1',
        child: []
    },{
        name: 'gc2',
    child:[]
    }]
}]},{
name: 'p2',
child:[{
    name: 'c1',
    child: [{
        name: 'gc1',
        child: []
    },{
        name: 'gc2',
    child:[]
    }]
},{
    name: 'c2',
    child: [{
        name: 'gc1',
        child: []
    },{
        name: 'gc2',
    child:[]
    }]
}]}]

Case 1: if search term is c1, the output is案例1:如果搜索词是c1,则output是

[{
    name: 'p1',
    child:[{
        name: 'c1',
        child: []
    }]
},{
    name: 'p2',
    child:[{
        name: 'c1',
        child: []
    }]
}]

Case 2: if user searches for c3, the output is案例2:如果用户搜索c3,output是

[{
    name: 'p1',
    child:[{
        name: 'c3',
        child: []
    }]
}]

Case 3: if user searches for p1, the output is案例 3:如果用户搜索 p1,则 output 是

[{
    name: 'p1',
    child:[]
}]

Case 4 If user searches for gc1, the output is案例 4如果用户搜索 gc1,则 output 是

[{name: 'p1',child:[{
name: 'c1',
child: [{
    name: 'gc1',
    child: []
}]},{
name: 'c2',
child: [{
    name: 'gc1',
    child: []
}]},{
name: 'c3',
child: [{
    name: 'gc1',
    child: []
}]}]},{name: 'p2', child:[{
name: 'c1',
child: [{
    name: 'gc1',
    child: []
}]},{
name: 'c2',
child: [{
    name: 'gc1',
    child: []
}]}]}]

Notice that if children name doesn't match, then it is not included in the output.请注意,如果子名称不匹配,则它不包含在 output 中。

I am using the following logic but it doesn't filter nodes 'n' level:我正在使用以下逻辑,但它不过滤节点'n'级别:

function filterTree(data, matcher) {
    data.child= data.child.filter(row => row.name === matcher);
    for (nodeIndex in data.child)
        filterTree(data.child[nodeIndex], matcher);

}

I need an optimal way to perform filter我需要一种最佳方式来执行过滤

You could reduce the arrays and look for the wanted name or loock for the child array and if an object is found add the actual object with the new child array.您可以减少 arrays 并查找想要的名称或查找child数组,如果找到 object,则将实际的 object 添加到新的子数组中。

 function search(array, name) { const s = (r, { child, ...object }) => { if (object.name === name) { r.push({ object, child: [] }); return r; } child = child.reduce(s, []); if (child.length) r.push({...object, child }); return r; }; return array.reduce(s, []); } var data = [{ name: 'p1', child: [{ name: 'c1', child: [{ name: 'gc1', child: [] }, { name: 'gc2', child: [] }] }, { name: 'c2', child: [{ name: 'gc1', child: [] }, { name: 'gc2', child: [] }] }, { name: 'c3', child: [{ name: 'gc1', child: [] }, { name: 'gc2', child: [] }] }] }, { name: 'p2', child: [{ name: 'c1', child: [{ name: 'gc1', child: [] }, { name: 'gc2', child: [] }] }, { name: 'c2', child: [{ name: 'gc1', child: [] }, { name: 'gc2', child: [] }] }] }]; console.log(search(data, 'c1')); console.log(search(data, 'c3')); console.log(search(data, 'p1')); console.log(search(data, 'gc1'));
 .as-console-wrapper { max-height: 100%;important: top; 0; }

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

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