2013-03-22 56 views
1

我有這個咖啡的腳本,我從這個問題,我問得更早。打破循環並返回結果一旦找到

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

回答

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結束「)

正如您所看到的,在」我找到它「之後,循環立即停止。

的擊打它沒有打破循環,從而設置結果爲空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

0

看起來像你需要一個又一個突破如果結果不爲空。事情是這樣的:

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

您需要打破你的循環,如果對象找到這個函數的遞歸調用中。