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

嵌套对象返回未定义的递归函数

  •  0
  • cup_of  · 技术社区  · 5 年前

    let myObj = [{
        id: 1,
        children: [{
            id: 1.1,
            children: []
          },
          {
            id: 1.2,
            children: []
          }
        ]
      },
      {
        id: 2,
        children: [{
            id: 2.1,
            children: []
          },
          {
            id: 2.2,
            children: []
          }
        ]
      }
    ]
    
    
    function addToObj(itemToAdd, parentId, obj) {
    
      for (let i = 0; i < obj.length; i++) {
    
        const item = search(obj[i], parentId);
    
        console.log(item); // undefined
    
        if (item) {
          item.children = item.children.concat(itemToAdd);
          break;
        }
      }
    
      function search(obj, id) {
        if (obj.id === id) {
          console.log(obj); // defined (obj with id of 2.1), but returns undefined?
          return obj;
        }
    
        for (let i = 0; i < obj.children.length; i++) {
          search(obj.children[i], id);
        }
      }
    
      return obj;
    };
    
    const itemToAdd = {
      id: 100,
    }
    
    addToObj(itemToAdd, 2.1, myObj);

    上面代码段中的函数在对象中循环,查找特定项。如果找到该项,它将在该项的子属性中插入一个对象。

    2 回复  |  直到 5 年前
        1
  •  1
  •   CertainPerformance    5 年前

    您需要使用递归函数的返回值 search :如果存在,则返回:

    for (let i = 0; i < obj.children.length; i++) {
      const possibleResult = search(obj.children[i], id);
      if (possibleResult) {
        return possibleResult;
      }
    }
    

    let myObj = [{
        id: 1,
        children: [{
            id: 1.1,
            children: []
          },
          {
            id: 1.2,
            children: []
          }
        ]
      },
      {
        id: 2,
        children: [{
            id: 2.1,
            children: []
          },
          {
            id: 2.2,
            children: []
          }
        ]
      }
    ]
    
    
    function addToObj(itemsToAdd, parentId, obj) {
    
      for (let i = 0; i < obj.length; i++) {
    
        const item = search(obj[i], parentId);
    
        // first log here will be undefined, nothing found
        // second log here will find the object
        console.log('item', item);
    
        if (item) {
          item.children = item.children.concat(itemsToAdd);
          break;
        }
      }
    
      function search(obj, id) {
        if (obj.id === id) {
          console.log('obj', obj); // defined (obj with id of 2.1), but returns undefined?
          return obj;
        }
    
        for (let i = 0; i < obj.children.length; i++) {
          const possibleResult = search(obj.children[i], id);
          if (possibleResult) {
            return possibleResult;
          }
        }
      }
    
      return obj;
    };
    
    const itemToAdd = {
      id: 100,
    }
    
    addToObj(itemToAdd, 2.1, myObj);
        2
  •  1
  •   Maheer Ali    5 年前

    • 如果 (obj.id === id) false
    • 你应该检查一下 obj.children 在循环之前存在。

    let myObj = [
      {
        id: 1,
        children: [
        	{
          	id: 1.1,
            children: []
          },
          {
          	id: 1.2,
            children: []
          }
        ]
      },
      {
        id: 2,
        children: [
          {
            id: 2.1,
            children: []
          },
          {
          	id: 2.2,
            children: []
          }
        ]
      }
    ]
    
    There are two problems in code:
    
     - List item
    
    function addToObj(itemToAdd, parentId, obj) {
    
      for (let i=0;i<obj.length;i++) {
    
        const item = search(obj[i], parentId);
    
        console.log(item); // undefined
    
        if (item) {
          item.children = item.children.concat(itemToAdd);
          break;
        }
      }
    
      function search(obj, id) {
        if (obj.id === id) {
          console.log(obj); // defined (obj with id of 2.1), but returns undefined?
          return obj;
        }
        if(obj.children){
        
        for (let i=0;i<obj.children.length;i++) {
           let x = search(obj.children[i], id);
           if(x) return x;
          }
        }
      }
    
      return obj;
    };
    
    const itemToAdd = {
    	id: 100,
    }
    
    addToObj(itemToAdd, 2.1, myObj);