简体   繁体   English

为什么在 javascript 的 for-in 循环内执行 return 语句后的代码?

[英]Why code after return statement executing inside for-in loop in javascript?

my variable look like this我的变量看起来像这样

var widgets2 = [{
    id: "1",
    title: 'title',
    children: [],
  },
  {
    id: "2",
    title: 'title2',
    children: []
  },
  {
    id: "3",
    title: 'title3',
    children: [{
      id: "4",
      title: 'title4',
      children: [],
    }, {
      id: "5",
      title: 'title5',
      children: [],
      children: [{
          id: "6",
          title: 'title6',
          children: [],
        },
        {
          id: "7",
          title: 'title7',
          children: [],
        }
      ]
    }],
  },
  {
    id: "9",
    title: 'title9',
    children: [],
  }
]

The function code look like this function 代码如下所示

function findTheKey(id,widget){
 let newObj=[...widget];
    for(var key in newObj){
        if(newObj[key]['id']==id){
        console.log(newObj[key])
        return newObj[key];
        }
        console.log("came here")
        if(newObj[key].hasOwnProperty("children")){
         findTheKey(id,newObj[key].children);
        }
    }
}

When the called the function using following code当使用以下代码调用 function

var result=findTheKey(4,widgets2);
console.log(result) 

The result look like this结果看起来像这样

{id: "4", title: "title4", children: Array(0)}
 came here

That means even after executing return statement, console.log getting executed, any help will be highly appreciated.这意味着即使在执行 return 语句、执行 console.log 之后,任何帮助都将受到高度赞赏。 Thankyou谢谢

Since this is a recursive function, the return does not have the effect you expect, you need a variable outside the recursive function to keep the current state of what you want to find.由于这是一个递归的function,所以返回并没有你期望的效果,你需要一个递归的function之外的变量来保持你想要找到的当前Z9ED39E2EA931586B6A985A6942EF573E

See the snippet below for example:例如,请参见下面的代码段:

 var widgets2 = [ { id: "1", title: "title", children: [], }, { id: "2", title: "title2", children: [], }, { id: "3", title: "title3", children: [ { id: "4", title: "title4", children: [], }, { id: "5", title: "title5", children: [], children: [ { id: "6", title: "title6", children: [], }, { id: "7", title: "title7", children: [], }, ], }, ], }, { id: "9", title: "title9", children: [], }, ]; let found; function findTheKey(id, widget) { let newObj = [...widget]; for (var key in newObj) { if (newObj[key]["id"] == id) { found = newObj[key]; break; } if (newObj[key].hasOwnProperty("children")) { findTheKey(id, newObj[key].children); } } return found; } var result = findTheKey(4, widgets2); console.log(result);

You should update your question for this, but since you asked for it in the comments, here is what I propose.您应该为此更新您的问题,但由于您在评论中要求,这就是我的建议。

function findTheKey(id, widget) {
  const newObj = [...widget];
  for (const key in newObj) {
    if (newObj[key]["id"] === `${id}`) {
        return newObj[key];
    }
    if (newObj[key].hasOwnProperty('children')) {
        /* Use the result of the recursive function */
        const foundObject = findTheKey(id, newObj[key].children);
        if(foundObject) return foundObject;
    }
  }
  return false;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM