Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to find object using recursion in javascript?

let tree = {
    name: "A",
    children: [
        {
            name: 'A-1',
            children: [
                {name: "A-1-A"},
                {name: "A-1-B"},
            ]
        },
        {
            name: 'B-1',
            children: [
                {
                    name: "B-1-A",
                    children: [
                        {name: "B-11-A"},
                        {name: "B-11-B"}
                    ]
                },
                {name: "B-1-B"},
            ]
        },

    ]


};

I am trying to find object from tree object using recursion .

when I call like this searchFn(tree,'A-1') it should return { name: 'A-1', children: [ {name: "A-1-A"}, {name: "A-1-B"}, ] } object

it I call like this searchFn(tree,'A-1-A') it should return this

{name: "A-1-A"}

I tried like this but not working

 function searchFn(obj ,searchText){
        if(obj.name === searchText) return obj
        if(obj.children.length > 0){
          return   searchFn(obj.children.pop(),searchText)
        }
      return null
    }
like image 336
user944513 Avatar asked Jan 25 '23 04:01

user944513


2 Answers

First, you should create a class for readability. Then you should have a loop inside the function that iterates over the children and only returns if a child is found.

    class Node {
        static fromStruct = (struct) => {
            const tree = new Node(struct.name);
            if(!struct.children) return tree;
            for(const child of struct.children) {
                tree.children.push(Node.fromStruct(child));
            }
            return tree;
        }

        constructor(name){
            this.name = name;
            this.children = [];
        }
    
        addChild = (parentName, childName) => {
            const parent = this.searchFn(parentName);
            if(!parent) throw new Error("Parent not found");
            parent.children.push(new Node(childName));
        }
    
        searchFn = (name) => {
            if(this.name === name) return this;
            for(const child of this.children) {
                const found = child.searchFn(name);
                if(found !== null) return found;
            }
            return null;
        }
    }


    const data = {
        name: "A",
        children: [
            {
                name: 'A-1',
                children: [
                    {name: "A-1-A"},
                    {name: "A-1-B"},
                ]
            },
            {
                name: 'B-1',
                children: [
                    {
                        name: "B-1-A",
                        children: [
                            {name: "B-11-A"},
                            {name: "B-11-B"}
                        ]
                    },
                    {name: "B-1-B"},
                ]
            },
    
        ]
    };

    const tree = Node.fromStruct(data);
    console.log(tree.searchFn("A-1-A"));
like image 28
Alexandre Senges Avatar answered Jan 27 '23 18:01

Alexandre Senges


You need to iterate the children of the object and take a variable for the result.

function searchFn(object, searchText) {
    var result;
    if (object.name === searchText) return object;
    (object.children || []).some(o => result = searchFn(o, searchText));
    return result || null;
}


let tree = { name: "A", children: [{ name: 'A-1', children: [{ name: "A-1-A" }, { name: "A-1-B" }] }, { name: 'B-1', children: [{ name: "B-1-A", children: [{ name: "B-11-A" }, { name: "B-11-B" }] }, { name: "B-1-B" }] }] };

console.log(searchFn(tree, 'foo'));
console.log(searchFn(tree, 'A-1'));
.as-console-wrapper { max-height: 100% !important; top: 0; }
like image 198
Nina Scholz Avatar answered Jan 27 '23 19:01

Nina Scholz