Js recursion cannot return data

        const tree = {
            id:"root",
            children:[
                {id:1,children:["id1"]},
                {id:2,children:["id2"]},
                {id:3,children:["id3"]},
                {id:4,children:[
                    {id:5,children:["id4id5"]}
                ]},
                {id:6,children:[
                    {id:7,children:[
                        {id:8,children:["id4id5"]}
                    ]}
                ]}
            ]
        }



        function search(id,arr){
            let asd = {};
            arr.forEach((val,ind,arr)=>{
                if(!val.id) {asd = "meiyou";return}    
                if(val.id == id){
                    asd["id"] = val.id;
                    asd["children"] = val.children;
                    console.log(asd); // 
                    return;
                }else{
                    search(id,val.children);
                }
            });
            return asd;
        }

        var result = search(5,tree.children);
        console.log(result); // {}
The first layer of the

array can return normally, and if it is deeper, it will return {} and ask for the correct answer

.
Apr.08,2022

there are still a lot of problems. Do not return in the callback function of forEach , because that is meaningless. It is a callback function and does not prevent forEach from continuing execution.
if id is not repeated, you don't need to traverse all the nodes, because that doesn't make sense.

        function search(id, node) {
            if (node.children) {
                for (let nc of node.children) {
                    if (nc.id === id) {
                        return nc;
                    } else {
                        let same = search(id, nc);
                        if (same) return same;
                    }
                }
            }
            return null;
        }


        var result = search(5, tree);
        console.log('result', result); // {}

the variable asd cannot be defined in the function search . In this way, each recursion will reassign the asd to {} , and the search function is also problematic. Please refer to the following code

    const tree = {
        id:"root",
        children:[
            {id:1,children:['id1']},
            {id:2,children:['id2']},
            {id:3,children:['id3']},
            {id:4,children:[
                {id:5,children:['id4id5']}
            ]},
            {id:6,children:[
                {id:7,children:[
                    {id:8,children:['id4id5']}
                ]}
            ]}
        ]
    }


    // 
    let asd = {}
    
    function search(id,arr){
        arr.forEach((val,ind,arr)=>{
            if(val.id && val.id == id){
                asd['id'] = val.id;
                asd['children'] = val.children;
                console.log(asd); // 
            }else{
                if(val.children){
                    search(id,val.children);
                }
            }
        });
        return asd;
    }

    var result = search(5,tree.children);
    console.log(result); // {id: 5, children: Array(1)}

because the asd after your second layer is the local variable of the inner layer, when you return, the outer layer does not get this value.

you can put asd outside the search function as a global variable;
, of course, it's better to return

with a value in the search.
Menu