Javascript 我真的必须为这个递归函数使用全局变量吗?
我有一个递归函数,它恰好是for循环Javascript 我真的必须为这个递归函数使用全局变量吗?,javascript,recursion,Javascript,Recursion,我有一个递归函数,它恰好是for循环 function deepFindGroup(groupName, currentGroup) { console.log("Testing"); for (e in currentGroup) { if (currentGroup[e].intName == groupName) { console.log(currentGroup[e]["Members"]); retur
function deepFindGroup(groupName, currentGroup) {
console.log("Testing");
for (e in currentGroup) {
if (currentGroup[e].intName == groupName) {
console.log(currentGroup[e]["Members"]);
return currentGroup[e]["Members"];
} else if (currentGroup[e]["Members"]) {
return deepFindGroup(groupName, currentGroup[e]["Members"]);
}
}
}
似乎我不能用一个有意义的返回语句来处理每一种可能性。例如,如果currentGroup[e].intName不等于groupName,并且该分支的子级没有成员属性(在本例中,它根本没有子级)。它最终返回未定义。我能想到的唯一解决办法是创建一个全局变量,我正试图避免这种情况
编辑:
函数应该返回树中的给定分支。树由包含“成员”的对象(具有intName属性)组成,这些成员本身可以是包含其他成员的对象。成员也可以不包含任何内容,但仍然具有intName
看起来是这样的:
Gengroup_1--intName: Gengroup_1
|
--Members-- nochild -- intName: nochild
|
--Gengroup_2--intName: Gengroup2
|
--Members-- object -- intName: object
|
-- anotherObject -- intName: anotherObject
如果一个分支未生成查找,则函数不会尝试多个分支。即使没有找到匹配项,它也会返回。相反,它应该允许
for
循环继续,以便可以通过另一个分支进行新的搜索
假设找到的值永远不会是未定义的
本身,您可以这样做:
function deepFindGroup(groupName, currentGroup) {
var result;
console.log("Testing");
for (var e in currentGroup) {
if (currentGroup[e].intName == groupName) {
console.log(currentGroup[e]["Members"]);
return currentGroup[e]["Members"];
} else if (currentGroup[e]["Members"]) {
// Don't return before you have looked at the result:
result = deepFindGroup(groupName, currentGroup[e]["Members"]);
if (result !== undefined) return result;
}
}
}
还要确保声明了变量。您没有声明
e
,因此它是全局的,因此函数作用域的不同递归实例将更改相同的e
变量,从而导致不稳定的行为。如果一个分支未生成查找,您的函数将不会尝试多个分支。即使没有找到匹配项,它也会返回。相反,它应该允许for
循环继续,以便可以通过另一个分支进行新的搜索
假设找到的值永远不会是未定义的
本身,您可以这样做:
function deepFindGroup(groupName, currentGroup) {
var result;
console.log("Testing");
for (var e in currentGroup) {
if (currentGroup[e].intName == groupName) {
console.log(currentGroup[e]["Members"]);
return currentGroup[e]["Members"];
} else if (currentGroup[e]["Members"]) {
// Don't return before you have looked at the result:
result = deepFindGroup(groupName, currentGroup[e]["Members"]);
if (result !== undefined) return result;
}
}
}
还要确保声明了变量。您没有声明
e
,因此它是全局的,因此函数作用域的不同递归实例将更改相同的e
变量,从而导致不稳定的行为。在输入函数时定义结果
,在发现某些内容时停止迭代:
函数deepFindGroup(groupName,currentGroup){
var结果=null;
对于(当前组中的e){
if(currentGroup[e].intName==组名){
结果=当前组[e][“成员”];
}else if(当前组[e][“成员”]){
结果=deepFindGroup(groupName,currentGroup[e][“Members”]);
}
如果(结果| |结果===未定义)中断;
}
返回结果;
}
变量组={
一:{
intName:'一',
成员:{
一(1):{
intName:'one_one',
}
}
},
二:{
intName:'两个',
成员:{
二,一:{
intName:'two_-one',
成员:{}
}
}
}
}
log(deepFindGroup('one',group))/->{one_one:{intName:'one_one'}
console.log(deepFindGroup('one_one',group))/>未定义(成员未定义)
log(deepFindGroup('two_-one',group))/->{}
console.log(deepFindGroup('two_two',group))/->null
定义结果
当进入函数时,在发现某些内容时停止迭代:
函数deepFindGroup(groupName,currentGroup){
var结果=null;
对于(当前组中的e){
if(currentGroup[e].intName==组名){
结果=当前组[e][“成员”];
}else if(当前组[e][“成员”]){
结果=deepFindGroup(groupName,currentGroup[e][“Members”]);
}
如果(结果| |结果===未定义)中断;
}
返回结果;
}
变量组={
一:{
intName:'一',
成员:{
一(1):{
intName:'one_one',
}
}
},
二:{
intName:'两个',
成员:{
二,一:{
intName:'two_-one',
成员:{}
}
}
}
}
log(deepFindGroup('one',group))/->{one_one:{intName:'one_one'}
console.log(deepFindGroup('one_one',group))/>未定义(成员未定义)
log(deepFindGroup('two_-one',group))/->{}
console.log(deepFindGroup('two_two',group))/->null
函数应该做什么?请返回特定于树的给定分支的位置。无论如何,您可能需要一个值来表示未找到任何内容(例如,null
),并在从子查找返回之前进行检查。您所说的“给定分支的位置”是什么意思?您想要路径还是实际组,而不参考位置?你也可以发布一个草堆的例子吗?我对数据结构是新手,如果这是一个的话。我试图在更新中提供更多信息。该函数应该做什么?请返回特定于树的给定分支的位置。无论如何,您可能需要一个值来表示未找到任何内容(例如,null
),并在从子查找返回之前进行检查。您所说的“给定分支的位置”是什么意思?您想要路径还是实际组,而不参考位置?你也可以发布一个草堆的例子吗?我对数据结构是新手,如果这是一个的话。我已经试着用我的更新提供更多的信息。谢谢你的e,我不知道。有没有官方消息可以证实这一点?@DominicGrenier这是一个非常有名的JS功能:如果你不使用var
关键字,它将在全局范围内声明。正如您所要求的官方来源,这里是:没有比ECMAScript的文档更正式的了。谢谢你的电子邮件,我不知道。有没有官方消息可以证实这一点?@DominicGrenier这是一个非常有名的JS功能:如果你不使用var
关键字,它将在全局范围内声明。正如您所要求的官方来源,这里是:没有比ECMAScript的文档更正式的了。