JavaScript-改进递归循环,根据单个条件返回值
我有一个方法,它将几个深度的数组(JavaScript-改进递归循环,根据单个条件返回值,javascript,recursion,Javascript,Recursion,我有一个方法,它将几个深度的数组(group)作为参数,将名称作为字符串(currentFilterName) 深度级别是元素。子元素,目标是返回元素。值,如果元素具有深度级别,则每次循环时,我首先检查element.name===currentFilterName如果是这种情况,我将返回元素.value如果不是这种情况,我将返回下一个元素的方法。如果有下一个索引this.filtersConfig的原始表,则返回childrent[index+1],如果元素没有深度,我只需检查element.
group
)作为参数,将名称作为字符串(currentFilterName
)
深度级别是元素。子元素
,目标是返回元素。值
,如果元素具有深度级别,则每次循环时,我首先检查element.name===currentFilterName
如果是这种情况,我将返回元素.value
如果不是这种情况,我将返回下一个元素的方法。如果有下一个索引this.filtersConfig的原始表,则返回childrent[index+1]
,如果元素没有深度,我只需检查element.name===currentFilterName
,然后返回element.value
我的问题是,这个方法有点冗长,我们如何用优雅的文字来简化它?下面是如何递归地遍历数据,根据
过滤器名称找到所需的值
checkFilterValue(group, currentFilterName) {
group.forEach((element, index) => {
if (element.children) {
if (element.name === currentFilterName) {
this.returnedValue = element.value
} else {
this.checkFilterValue(element.children || this.filtersConfig[index + 1], currentFilterName)
}
} else {
if (element.name === currentFilterName) {
this.returnedValue = element.value
}
}
})
}
forEach
的问题是不能提前退出,但可以使用for
循环。可以在for
循环中使用break;
和continue;
提前退出,或跳过迭代
函数检查过滤器值(数据、过滤器名称){
让结果;
常量findValue=(数据,过滤器名称)=>{
for(设i=0;i console.log(checkFilterValue(data,'c'));
匿名sbs答案非常简洁,但你们两个都有不完全必要的代码,他封装了递归部分,而我选择的不是o。无论如何,递归方法可能是最好的
function checkFilterValue(group, currentFilterName) {
for (let i = 0, element; element = group[i]; i++) {
// use "return" to cancel loop early and return value
// no need for else clause or break
if (element.name === currentFilterName) {
return this.returnedValue = element.value
}
// now we need to check if there are childern
// not sure what filterConfig is, though, you may want to test this
// index propably is the depth, and not the index of the loop
let children = element.children || this.filtersConfig[i + 1]
if (children) {
return this.checkFilterValue(children, currentFilterName)
}
}
}
我建议使用,以便您可以在需要时停止迭代。当您提供的回调函数返回true时,它将停止。
此外,一些ifs可以简化。您可以对有子元素或没有子元素的元素执行element.name检查,它似乎可以单独放置
checkFilterValue(group, currentFilterName) {
return group.some((element, index) => {
if (element.name === currentFilterName) {
this.returnedValue = element.value
return true;
}
else if (element.children) {
return this.checkFilterValue(element.children || this.filtersConfig[index + 1], currentFilterName);
}
else
return false;
}
)
}
如果您有element.children | | this.filtersConfig[index+1]
元素.children
的值总是真实的,因此第二个表达式永远不会被计算。这意味着this.filtersConfig
在您的算法中不起任何作用
因为您指出您的代码产生了预期的结果,并且只需要更优雅的代码,所以应该从代码中排除该部分
下面是一个arrow函数,它将查找该值,但当它这样做时,它也将停止迭代和递归,这与您的代码相反:
const checkFilterValue = (group, currentFilterName) =>
group && group.some(element =>
element.name === currentFilterName
? (this.returnedValue = element.value, true)
: this.checkFilterValue(element.children, currentFilterName)
);
你确定行
this.returnedValue=element.value
运行吗?你能给出一个输入的例子吗?@CertainPerformance这就是问题所在,我改变了问题,我想用更优雅的文字来改进它。请注意,在你的代码this.filtersConfig[index+1]
永远不会被计算,因为元素。子元素在那个地方总是真实的。您能详细说明我缺少返回语句的地方吗?我的函数返回值,或者未定义(我的结果变量的默认值)此外,在for循环中使用return将退出for循环,但也将退出函数,OP可能希望在for循环之后,但在函数结束之前执行更多操作。@AnonymousSB对不起,您的代码现在看起来很好,我必须检查您的代码的早期版本,其中由于在处缺少return语句,递归无法工作递归调用。@AnonymousSB关于第二点;我通常认为应该尽可能地分解函数。小函数是可重用的,更便于他人复制,更重要的是也便于测试。在任何情况下,可以在我的代码中第一个返回语句之前添加更多代码。该方法可以直接返回吗n不带指定变量的元素.value
?当然。您尝试过了吗?更改上面的代码以实现这一点并不难。尝试一下吧…提示:使用findIndex
而不是some
,并对结果进行处理。请注意,后续问题确实应该作为新问题发布。