一个Javascript函数,用于过滤带有搜索字词的树状json.排除与搜索词不匹配的任何对象 [英] A Javascript function to filter tree structured json with a search term. exclude any object which donot match the search term

查看:38
本文介绍了一个Javascript函数,用于过滤带有搜索字词的树状json.排除与搜索词不匹配的任何对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个具有以下结构的json,它可以达到n级深度:

I am havin a json with following structure which can go up to n level depth:

[{
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:[]
    }]
}]}]

情况1::如果搜索词为c1,则输出为

Case 1: if search term is c1, the output is

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

情况2::如果用户搜索c3,则输出为

Case 2: if user searches for c3, the output is

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

情况3::如果用户搜索p1,则输出为

Case 3: if user searches for p1, the output is

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

案例4 :如果用户搜索gc1,则输出为

Case 4 If user searches for gc1, the output is

[{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.

我使用以下逻辑,但是它不过滤节点"n"级:

I am using the following logic but it doesn't filter nodes 'n' level:

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

推荐答案

您可以缩小数组,并为 child 数组查找所需的名称或loock,如果找到了对象,则添加实际的具有新的子数组的对象.

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.

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; }

这篇关于一个Javascript函数,用于过滤带有搜索字词的树状json.排除与搜索词不匹配的任何对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆