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

2 回答 2

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
于 2013-03-22T11:01:34.423 回答
0

如果结果不为空,您似乎需要再休息一次。像这样的东西:

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

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

于 2013-03-22T11:01:02.657 回答