繁体   English   中英

递归函数返回未定义的值

[英]Recursive function returning undefined value

我想从多级结构中获取对象

我为它编写了函数但是即使返回它不是从函数和返回值中出来的,它继续下一次递归。 我知道它返回到以前调用的函数的值,因为它的作用域阻止它被覆盖,这就是为什么返回未定义的值

  var selectedObj = findObjectByUid( existingStructure, selectedUid);
function findObjectByUid( root, selectedUid ) {
    if( root.uniqueId === selectedUid ) {
        return root;
    }
    if( root.children && root.children.length > 0 ) {
        for( var k in root.children ) {
            if( root.children[ k ].uniqueId === selectedUid ) {
                return root.children[ k ];
            } else if( root.children.length ) {
                return findObjectByUid( root.children[ k ], selectedUid );
            }
        }
    }
}

这里我想回到我的初始调用函数,当它匹配uid。

实际上,无论找到的节点如何,您都会与第一个孩子一起返回。

您可以使用临时变量并存储子检查的结果,如果不是,则返回此值。

顺便说一句,您可以直接将数组的子进行递归。

function findObjectByUid(root, selectedUid) {
    if (root.uniqueId === selectedUid) return root;
    if (!root.children || !root.children.length) return;
    for (let child of root.children) {
        let temp = findObjectByUid(child, selectedUid);
        if (temp) return temp;
    }
}

var selectedObj = findObjectByUid(existingStructure, selectedUid);

在阵列上使用此方法有三个问题。 首先,如果这些属性是可枚举的,for ... in也会迭代对象的原型属性。 例如:

Array.prototype.voice = "James Earl Jones";

var tMinus = [
  "Two",
  "One",
  "Blast off!"
];

var countdown = "";

for (var step in tMinus) {
  countdown += tMinus[step] + "\n";
}

console.log(countdown);
// => "Two
//    One
//    Blast Off!
//    James Earl Jones
//    "

这可以通过使用hasOwnProperty来排除原型属性来解决。 例:

for (var step in tMinus) {
  if (tMinus.hasOwnProperty(step)) {
    countdown += tMinus[step] + "\n";
  }
}

这是更正后的代码。 您已经在内部调用中使用了return findObjectByUid ,代码在完成循环之前就已经终止了。

function findObjectByUid( root, selectedUid ,foundArr) {

  if( root.uniqueId === selectedUid ) {
    foundArr.push(root);
      return root;
  }
  else if( root.children && root.children.length > 0 ) {
      for( var k in root.children ) {
           findObjectByUid( root.children[k], selectedUid,foundArr ); 
          if(root.children[k]=== selectedUid){break;}
      }
  }
   return foundArr.length>0?foundArr[0]:null;
}

示例json和调用方法

var root = {uniqueId:1,children:[{uniqueId:10},{uniqueId:11,children:[{uniqueId:21,children:[]},{uniqueId:22,children:[]},{uniqueId:23,children:[{uniqueId:31,children:[]},{uniqueId:32,children:[]}]}]},{uniqueId:12,children:[]},{uniqueId:13,children:[]}]};
findObjectByUid(root,32,[]);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM