【发布时间】:2019-09-06 17:10:37
【问题描述】:
我想从多层结构中获取对象
我为它编写了函数,但即使在返回时它也没有从函数中出来并返回值,它会继续下一次递归。我知道它对先前调用的函数的返回值,并且由于它的作用域是阻止它被覆盖,这就是返回未定义值的原因
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 时回到我的初始调用函数。
【问题讨论】:
-
你能添加一个示例 JSON 吗?
标签: javascript