代码之家  ›  专栏  ›  技术社区  ›  user944513

如何在javascript中使用递归查找对象?

  •  1
  • user944513  · 技术社区  · 6 年前
    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"},
                ]
            },
    
        ]
    
    
    };
    

    object 从 tree object 使用递归。

    searchFn(tree,'A-1') 它应该会回来 { name: 'A-1', children: [ {name: "A-1-A"}, {name: "A-1-B"}, ] }

    我这样叫它 searchFn(tree,'A-1-A') 它应该还这个

    {name: "A-1-A"}

    我试过了但没用

     function searchFn(obj ,searchText){
            if(obj.name === searchText) return obj
            if(obj.children.length > 0){
              return   searchFn(obj.children.pop(),searchText)
            }
          return null
        }
    
    2 回复  |  直到 6 年前
        1
  •  4
  •   Nina Scholz    6 年前

    您需要迭代对象的子对象并为结果获取一个变量。

    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; }
        2
  •  2
  •   Alexandre Senges    6 年前

        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"));
        3
  •  1
  •   xdeepakv    6 年前

    const 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"}]}]}
    
    const searchFn = (tree, name) => {
      let result = null;
      if (typeof tree !== "object") return result;
      if (tree.name === name) return tree;
      if (tree.children && tree.children.length) {
        tree.children.some(data => (result = searchFn(data, name)));
      }
      return result;
    };
    console.log(searchFn(tree, "A-1-A"));
    console.log(searchFn(tree, "A-1"));
    console.log(searchFn(tree, ""));
    .as-console-row {color: blue!important}
        4
  •  1
  •   Code Maniac    6 年前

    你可以试试这样的东西

    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" }] }] };
    
    function searchFn(obj, text){
      if(obj.name === text) return obj
      else{
        if(obj && obj.children && Array.isArray(obj.children)){
          for(let value of obj.children){
            if(searchFn(value, text)){
              return searchFn(value, text)
            }
          }
        }
      }
      return null
    }
    
    console.log(searchFn(tree, 'A-1'))
    console.log(searchFn(tree, 'A-1-A'))
    console.log(searchFn(tree, 'A-A-A'))
        5
  •  0
  •   Param Siddharth    6 年前

    函数的问题是它只检查 children

    function searchFn(obj ,searchText) { 
        if(obj.name === searchText) return obj; 
        if(obj.children) {
            for (let x of obj.children) {
                let y = searchFn(x,searchText);
                if (y)
                    return y;
            }
        }
        return null; 
    }