如何打破递归函数的循环?

如何打破递归函数的循环?,第1张

如何打破递归函数的循环?

尝试

function findCategory(categoryName) {    var trail = [];    var found = false;    function recurse(categoryAry) {        for (var i = 0; i < categoryAry.length; i++) { trail.push(categoryAry[i].category); // Found the category! if ((categoryAry[i].category === categoryName)) {     found = true;     break;     // Did not match... } else {     // Are there children / sub-categories? YES     if (categoryAry[i].children.length > 0) {         recurse(categoryAry[i].children);         if(found){  break;         }     } } trail.pop();        }    }    recurse(catalog);    return trail}

演示:小提琴



欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5652306.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-16
下一篇 2022-12-16

发表评论

登录后才能评论

评论列表(0条)

保存