怀疑人生...这是我遇过最诡异的问题,js递归查找到树的路径后for循环竟然无法停止

qq_26628129 2018-07-28 02:52:49

var treeData = [{"id":1,"pid":-1,"order":0,"children":[{"id":2,"pid":1,"order":0,"children":[{"id":3,"pid":2,"order":0,"children":[{"id":4,"pid":3,"order":0,"children":[{"id":16,"pid":4,"order":0,"children":null,"icon":null,"enabled":true,"description":""},{"id":17,"pid":4,"order":1,"children":null,"icon":null,"enabled":true,"description":""},{"id":18,"pid":4,"order":2,"children":null,"icon":null,"enabled":true,"description":""},{"id":19,"pid":4,"order":3,"children":null,"icon":null,"enabled":true,"description":""},{"id":20,"pid":4,"order":4,"children":null,"icon":null,"enabled":true,"description":""}],"icon":null,"enabled":true,"description":""},{"id":5,"pid":3,"order":1,"children":null,"icon":null,"enabled":true,"description":""},{"id":6,"pid":3,"order":2,"children":null,"icon":null,"enabled":true,"description":""},{"id":7,"pid":3,"order":3,"children":null,"icon":null,"enabled":true,"description":""},{"id":8,"pid":3,"order":4,"children":null,"icon":null,"enabled":true,"description":""},{"id":9,"pid":3,"order":5,"children":null,"icon":null,"enabled":true,"description":""},{"id":10,"pid":3,"order":6,"children":null,"icon":null,"enabled":true,"description":""},{"id":12,"pid":3,"order":7,"children":[{"id":29,"pid":12,"order":0,"children":null,"icon":null,"enabled":true,"description":""},{"id":30,"pid":12,"order":1,"children":null,"icon":null,"enabled":true,"description":""},{"id":31,"pid":12,"order":2,"children":null,"icon":null,"enabled":true,"description":""},{"id":32,"pid":12,"order":3,"children":null,"icon":null,"enabled":true,"description":""}],"icon":null,"enabled":true,"description":""},{"id":13,"pid":3,"order":8,"children":[{"id":21,"pid":13,"order":0,"children":null,"icon":null,"enabled":true,"description":""},{"id":22,"pid":13,"order":1,"children":null,"icon":null,"enabled":true,"description":""},{"id":23,"pid":13,"order":2,"children":null,"icon":null,"enabled":true,"description":""},{"id":24,"pid":13,"order":3,"children":null,"icon":null,"enabled":true,"description":""}],"icon":null,"enabled":true,"description":""},{"id":14,"pid":3,"order":9,"children":[{"id":33,"pid":14,"order":0,"children":null,"icon":null,"enabled":true,"description":""},{"id":34,"pid":14,"order":1,"children":null,"icon":null,"enabled":true,"description":""},{"id":35,"pid":14,"order":2,"children":null,"icon":null,"enabled":true,"description":""},{"id":36,"pid":14,"order":3,"children":null,"icon":null,"enabled":true,"description":""}],"icon":null,"enabled":true,"description":""},{"id":15,"pid":3,"order":10,"children":null,"icon":null,"enabled":true,"description":""}],"icon":null,"enabled":true,"description":""}],"icon":null,"enabled":true,"description":""}],"icon":null,"enabled":true,"description":""}];
var idPathsFlag = false, idPaths = [], organizationId = 34;
var getIdPaths2 = function (data) {//#1
if(idPathsFlag === true) {
return false;
}
// console.log('!!!');
for (let i = 0; i < data.length; i++) {
idPaths.push(data[i].id);
if (data[i].id === organizationId) {
idPathsFlag = true;
console.log('break ', idPaths);
// debugger
//break后,程序又跳到for循环哪一行
break;
}
//如果是枝干
if (data[i].children && data[i].children.length !== 0) {
getIdPaths2(data[i].children);
} else {//如果是叶子
idPaths.pop();
if (i === data.length - 1) {
idPaths.pop();
}
}
}
}
getIdPaths2(treeData);
console.log(idPaths);

我在控制台在break语句打了断点,然后单步执行了几步,发现又跳回for循环那段了...已经被整的怀疑自己到底懂不懂js了...有没哪位大神指教一下
...全文
204 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
天际的海浪 2018-07-28
  • 打赏
  • 举报
回复
因为你是递归啊,getIdPaths2函数是多层次递归调用,break只是跳出当前层递归中的for循环。之后会退出当前层递归,返回到上一层递归的getIdPaths2(data[i].children);后面继续执行。如果上一层递归中的for循环没有循环完当然还要接着继续循环。

想要退出整个递归,就要一层一层的往上逐层退出

var getIdPaths2 = function (data) {//#1
// console.log('!!!');
for (let i = 0; i < data.length; i++) {
idPaths.push(data[i].id);
if (data[i].id === organizationId) {
idPathsFlag = true;
console.log('break ', idPaths);
debugger
//break后,程序又跳到for循环哪一行
break;
}
//如果是枝干
if (data[i].children && data[i].children.length !== 0) {
getIdPaths2(data[i].children);
if(idPathsFlag === true) {
return false;
}
} else {//如果是叶子
idPaths.pop();
if (i === data.length - 1) {
idPaths.pop();
}
}
}
}

87,843

社区成员

发帖
与我相关
我的任务
社区描述
Web 开发 JavaScript
社区管理员
  • JavaScript
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧