中断for循环并返回找到的结果

Break for loop and return result once found

本文关键字:结果 返回 for 循环 中断      更新时间:2023-09-26

我有这个从我之前问的这个问题中得到的草稿。

window.getObject = (theObject, key, val) ->
  result = null
  if theObject instanceof Array
    i = 0
    while i < theObject.length
      result = getObject(theObject[i], key, val)
      i++
  else
    for prop of theObject
      return theObject  if theObject[prop] is val  if prop is key
      result = getObject(theObject[prop], key, val)  if theObject[prop] instanceof Object or theObject[prop] instanceof Array
  result

在这里找到结果:

return theObject  if theObject[prop] is val  if prop is key

现在它需要停止递归并返回结果。但它没有跳出循环,因此将结果再次设置为空。我肯定我错过了一些愚蠢的东西!

编辑

现在我改变了,所以我认为这会起作用

window.getObject = (theObject, key, val) ->
  result = null
  if theObject instanceof Array
    i = 0
    while i < theObject.length
      result = getObject(theObject[i], key, val)
      i++
  else
    for prop of theObject
      if theObject[prop] is val and prop is key
        result = theObject 
        console.log "I found it"
        break
      console.log "I must not log after found it was logged"
      result = getObject(theObject[prop], key, val)  if theObject[prop] instanceof Object or theObject[prop] instanceof Array
    console.log "stop!!"
  result

日志如下所示:

I must not log after found it was logged ui.js:49
I must not log after found it was logged ui.js:49
I must not log after found it was logged ui.js:49
stop!! ui.js:54
stop!! ui.js:54
I must not log after found it was logged ui.js:49
I must not log after found it was logged ui.js:49
I must not log after found it was logged ui.js:49
I found it ui.js:46
stop!! ui.js:54
I must not log after found it was logged ui.js:49
I must not log after found it was logged ui.js:49
I must not log after found it was logged ui.js:49
stop!! ui.js:54
stop!! ui.js:54
stop!! ui.js:54
stop!! ui.js:54
I must not log after found it was logged ui.js:49
stop!! ui.js:54
I must not log after found it was logged ui.js:49
stop!! ui.js:54
stop!! ui.js:54
stop!!

日志是正确的,显示了递归调用:

 I must not log after found it was logged ui.js:49
     I must not log after found it was logged ui.js:49
         I must not log after found it was logged ui.js:49
             stop!! ui.js:54
         stop!! ui.js:54
     I must not log after found it was logged ui.js:49
         I must not log after found it was logged ui.js:49
             I must not log after found it was logged ui.js:49
                 I found it ui.js:46
                stop!! ui.js:54
             I must not log after found it was logged ui.js:49
                 I must not log after found it was logged ui.js:49
                     I must not log after found it was logged ui.js:49
                    stop!! ui.js:54
                stop!! ui.js:54
            stop!! ui.js:54
        stop!! ui.js:54
     I must not log after found it was logged ui.js:49
        stop!! ui.js:54
     I must not log after found it was logged ui.js:49
        stop!! ui.js:54
    stop!! ui.js:54
stop!!

(忽略数组部分,在每次"I must not log"之后调用另一个递归级别,并且每次调用以"stopp"结束)

如你所见,在输入"I found it"之后,循环立即停止。

但是它没有跳出循环,因此再次将result设置为null

这发生在更高的层次上。您很高兴地分配result = getObject(…),但是在这里如果递归调用导致某些东西(在数组循环中也是如此),您不会中断。

然而,而不是维护一个result变量,我发现早期的回报更容易阅读(你不需要break):

window.getObject = (theObject, key, val) ->
  if theObject instanceof Array
    for item in theObject
      result = getObject(item, key, val)
      return result  if result
  else
    for prop, item of theObject
      return theObject  if item is val and prop is key
      result = getObject(item, key, val)  if item instanceof Object or item instanceof Array
      return result  if result
  null

如果结果不为空,看起来您需要另一个break。像这样:

result = getObject(theObject[prop], key, val)  if theObject[prop] instanceof Object or theObject[prop] instanceof Array
break if result not null

如果在此函数的递归调用中发现对象,则需要中断循环