首页 > 建站教程 > JS、jQ、TS >  js 递归查找多级数组对象所有匹配的项正文

js 递归查找多级数组对象所有匹配的项

js通过递归方式,查找到复杂的多级对象数组中,所有匹配的项,并返回结果。假设有以下数组:
departMentList:[
    {
        id:'0',
        label:'XX市应急局',
        isOpen:true,
        children:[
            {
                id:'1',
                label:'局领导',
                isOpen:true,
                children:[
                    {id:'11',label:'张三',isOpen:true,},
                    {
                        id:'12',
                        label:'武陵区应急局',
                        isOpen:true,
                        children:[
                            {id:'31',label:'李四',isOpen:true,},
                            {id:'32',label:'王三',isOpen:true,},
                            {id:'9527',label:'李世明',isOpen:true,},
                        ]
                    },
                    {id:'13',label:'王五',isOpen:true,},
                ]
            },
            {
                id:'2',
                label:'安全生产基础科',
                isOpen:true,
                children:[{id:'21',label:'王小二',isOpen:true,}]
            }
        ]
    },
    {
        id:'1',
        label:'XX市应急局',
        isOpen:true,
        children:[
            {
                id:'2',
                label:'局领导',
                isOpen:true,
                children:[
                    {id:'3',label:'张三',isOpen:true,},
                    {
                        id:'4',
                        label:'武陵区应急局',
                        isOpen:true,
                        children:[
                            {id:'31',label:'李四',isOpen:true,},
                            {id:'32',label:'王三',isOpen:true,},
                            {id:'9527',label:'李元霸',isOpen:true,},
                        ]
                    },
                    {id:'5',label:'王五',isOpen:true,},
                ]
            },
            {
                id:'6',
                label:'安全生产基础科',
                isOpen:true,
                children:[{id:'7',label:'王小二',isOpen:true,}]
            }
        ]
    },
]
分为两种情况,
1、查找到一条匹配,就立马返回,一般ID不会重复,所以用下面这种递归:
function find(arr, id) {
    if (arr == null) return null;
    for (let obj of arr) {
        if (obj.id === id) {
            return collect(obj);
        }
        let ret = find(obj.children, id);
        if (ret) return ret;
    }
    return null;
}
function collect(obj) {
    let ret = obj;
    if (obj.children) {
        for (let o of obj.children) {
            ret = [...ret, ...collect(o)]
        }
    }
    return ret;
}
console.log(find(departMentList,'9527'))
输出结果:



2、如果需要匹配所有的数据,以数组形式返回,就用这种递归,不要提前结束,一直循环完为止:
var result = [];
function find(arr, id) {
    if (arr == null) return null;
    for (let obj of arr) {
        if (obj.id === id) {
            result = [...result, collect(obj)];
        }
        find(obj.children, id);
    }
}
function collect(obj) {
    let ret = obj;
    if (obj.children) {
        for (let o of obj.children) {
            ret = [...ret, ...collect(o)]
        }
    }
    return ret;
}
find(departMentList,'9527')
console.log(result)
输出结果: