中断循环并返回结果一旦找到

时间:2013-03-22 10:11:59

标签: javascript coffeescript

我有这个coffeescript,我从之前问过的这个问题中得到了。

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

现在需要停止递归并返回结果。但它没有突破循环,因此将结果设置为null agian。我确定我错过了一些愚蠢的东西!

修改

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

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!!

2 个答案:

答案 0 :(得分:1)

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

 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!!

(忽略数组部分,在每次“我必须不记录”之后调用另一个递归级别,并且每个调用以“stopp”结束)

正如您所看到的,在“我找到它”之后,循环立即停止。

  

但它没有突破循环,因此将结果设置为null agian

这发生在下一个更高级别。您很乐意分配result = getObject(…),但 here 如果递归调用导致某些事情(在数组循环中它是相同的),您就不会破坏。

但是,我发现早期的返回更容易阅读(而且您不需要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

答案 1 :(得分:0)

如果结果不为null,您似乎需要另一个中断。像这样:

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

如果在此函数的递归调用中找到了对象,则需要中断循环。