打破循环并返回结果一旦找到

问题描述:

我有这个咖啡的脚本,我从这个问题,我问得更早。打破循环并返回结果一旦找到

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 

现在需要停止递归,并返回结果。但它不会摆脱循环,因此将结果设置为空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!! 
+0

要停止循环,使用关键字'break' – George 2013-03-22 10:14:57

+0

这样吗?如果对象[prop]是val 如果prop是关键 return theObject break – Harry 2013-03-22 10:16:05

+0

如果你从整个函数中执行'return',则不需要'循环'循环 – Bergi 2013-03-22 10:24:45

日志是正确的,展示你的递归调用:

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结束“)

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

的击打它没有打破循环,从而设置结果为空agian

那天正好在下一个更高的水平。你很高兴地分配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 
+0

Shweeet,谢谢你!这工作,看起来好多了 – Harry 2013-03-22 11:16:21

看起来像你需要一个又一个突破如果结果不为空。事情是这样的:

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

您需要打破你的循环,如果对象找到这个函数的递归调用中。