中断for循环并返回找到的结果
Break for loop and return result once found
我有这个从我之前问的这个问题中得到的草稿。
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
如果在此函数的递归调用中发现对象,则需要中断循环
相关文章:
- 是否可以将JXBrowser显示的JS确认对话框的结果返回到调用它的JS部分
- 将字符串结果返回到表单元格中的新行中
- 如何从批处理文件调用一个带有2个参数的java脚本函数,并将结果返回到环境变量
- 使用 JSON 和 ExtJS 将 MPDF 结果返回给浏览器
- 将信息结果返回到多个输入字段
- 如何将变量从javascript发送到php并将结果返回给JS
- 将 JavaScript 变量传递给 PHP,并将 PHP 结果返回给 JavaScript 变量
- 当没有结果返回 JavaScript 搜索时显示消息
- 加载相同的结果返回按钮
- 如果搜索框位于_SiteLayout页(在页眉 DIV 中),如何将搜索结果返回给用户
- MVC C#中的自定义操作结果返回特定视图
- 使用javascript/lodash对特定键的相同值进行分组,并对其进行计数,然后将结果返回到数组
- 如何使用javascript将循环结果返回到列中
- 结果返回为“未定义”
- 搜索未检测到结果返回-Selectize.js
- 调用一个打开活动并将活动结果返回给javascript的java函数
- 将PHP结果返回到原始html页面
- ASP.. NET MVC控制器结果返回到视图
- 如何通过AJAX传递值来更改SQL搜索查询并将结果返回给HTML ?
- Asp.net MVC或javascript将另一个页面的结果返回给方法