On js Recursive method

topic description

there are 2 arrays

related codes

var a = ["customer","supplier","materal","purchaseOrder","rolesMenge"]
var b = [
    {
        name:"maindata",
        children:[
            {
                name:"customer",
                meta:{
                    title:"customer"    
                }
            },
            {
                name:"supplier",
                meta:{
                    title:"supplier"    
                }
            },
            {
                name:"materal",
                meta:{
                    title:"materal"    
                }
            },
        ]
    },
    {
        name:"purchase",
        children:[
            {
                name:"purchaseOrder",
                meta:{
                    title:"purchaseOrder"    
                }
            },
            {
                name:"purchaseGood",
                meta:{
                    title:"purchaseGood"    
                }
            },
        ]
    },
    {
        name:"stock",
        children:[
            {
                name:"stockOrder",
                meta:{
                    title:"stockOrder"    
                }
            }
        ]
    },
    {
        name:"config",
        children:[
            {
                name:"userConfig",
                children:[
                    {
                        name:"rolesMenge",
                        meta:{
                            title:"rolesMenge"    
                        }
                    }
                ]
            },
        ]
    }
]

my code

function getarr(a,b){
    return b.reduce((k,m) => {
        if(m.children){
            let obj = {
                name:m.name,
                children:[]
            }
            for(let j of m.children){
                if(j.children){
                    getarr(a,m.children)
                } else {
                    if(a.includes(j.meta.title)){
                        obj.children.push(j)
                    }
                }
                
            }
            if(obj.children.length){
                k.push(obj)
            }

        }
        return k
    },[])
}

what result do you expect? What is the error message actually seen?

want to get

[
    {
        name: "maindata",
        children:[
            {
                name:"customer",
                meta:{
                    title:"customer"    
                }
            },
            {
                name:"supplier",
                meta:{
                    title:"supplier"    
                }
            },
            {
                name:"materal",
                meta:{
                    title:"materal"    
                }
            }
         ]
     }, 
     {
        name:"purchase",
        children:[
            {
                name:"purchaseOrder",
                meta:{
                    title:"purchaseOrder"    
                }
            }
        ]
    },
    {
        name:"config",
        children:[
            {
                name:"userConfig",
                children:[
                    {
                        name:"rolesMenge",
                        meta:{
                            title:"rolesMenge"    
                        }
                    }
                ]
            },
        ]
    }
]
Jun.24,2022

answered another similar question not long ago. I thought it was the same. After careful analysis, I found that there is a significant difference between the two questions:

  • this problem requires the result to retain the original tree structure, that is, all nodes on the parent path of eligible nodes should be retained

the new deal () is modified as follows (with comments), by the way, it deals with the problem that the result of the previous problem contains a lot of empty children

.
    for (const node of nodes) {
        const subs = deal(node.children, predicate);

        // 
        // 1.  subs 
        // 2. 
        if ((subs && subs.length) || predicate(node)) {
            node.children = subs;
            newChildren.push(node);
        }
    }
MySQL Query : SELECT * FROM `codeshelper`.`v9_news` WHERE status=99 AND catid='6' ORDER BY rand() LIMIT 5
MySQL Error : Disk full (/tmp/#sql-temptable-64f5-1b2b3ad-2ba6e.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
MySQL Errno : 1021
Message : Disk full (/tmp/#sql-temptable-64f5-1b2b3ad-2ba6e.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
Need Help?