Javascript 如何突破 .each() 并为函数返回值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3946381/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to break out of .each() and return a value for a function
提问by ParoX
I want to use return false
to break a .each() but also return a value at the same time. How can I do this?
我想用来return false
打破 .each() 但同时也返回一个值。我怎样才能做到这一点?
Please refer to a work-around function to see what I am trying to do:
请参阅变通功能以了解我正在尝试执行的操作:
function HasStores(state) {
var statehasstores = false;
$(stores).each(function (index, store) {
if (state == store.state && store.category == "meyers") {
statehasstores = true;
return false; // break
}
});
return statehasstores;
}
What Id like to do in pseudo code is:
我喜欢用伪代码做的是:
Function () {
for() {
if found {
return true;
}
}
return false;
}
采纳答案by ?ime Vidas
You're doing it right...
你做得对...
Quote from http://api.jquery.com/each/
"We can stop the loop from within the callback function by returning false."
“我们可以通过返回 false 从回调函数中停止循环。”
回答by mhitza
Be creative:
要有创意:
try {
$(stores).each(function (index, store) {
if(state == store.state && store.category == "meyers"){
throw store;
}
});
}
catch(e) {
// you got e with the value
}
No, I was just kidding, don't use this :). It came as an idea I liked to share.
不,我只是在开玩笑,不要使用这个:)。这是我喜欢分享的一个想法。
回答by BrunoLM
Use a variable outside the loop to get the value and use it afterward.
使用循环外的变量来获取值并在之后使用它。
var value;
$(stores).each(function (index, store) {
if(state == store.state && store.category == "meyers"){
statehasstores = true;
value = store; // added line
return false; //break
}
});
alert(value);
The way you're doing is just fine. I've tested on jsFiddle, see an example here.
你这样做的方式很好。我已经在 jsFiddle 上进行了测试,请在此处查看示例。
It's not working for you? Can you show more context?
它不适合你?你能展示更多的上下文吗?
回答by Mark Bessey
Alternatively, you could use a for
loop instead of each()
, and just return the value.
或者,您可以使用for
循环代替each()
,然后只返回值。
回答by Koobz
What you're suggesting is the way to do it. I'd think of it less as a workaround and more as an idiom.
你的建议是这样做的方法。我认为它不是一种解决方法,而是一种习惯用法。
回答by mike rodent
How about:
怎么样:
$.each( myObj, function( key, value ){
...
if( sthg... ){
myObj.somethingWentHorriblyWrong = true;
return false;
}
});
if( myObj.somethingWentHorriblyWrong ){
// ... do something, not forgetting to go:
delete myObj.somethingWentHorriblyWrong;
}
PS I was initially interested in what $.each(...
actually returns. As it says on the relevant JQ page, "The method returns its first argument, the object that was iterated", but in fact the solution doesn't even require that you use that fact...
PS我最初对$.each(...
实际返回的内容感兴趣。正如相关 JQ 页面上所说,“该方法返回它的第一个参数,即被迭代的对象”,但实际上该解决方案甚至不需要您使用该事实......
PPS Need a function that returns a value? Wrap in an outer function of course.
PPS 需要一个返回值的函数吗?当然包裹在一个外部函数中。
回答by elkolotfi
Okay I guess there's a little doubt about this point so maybe I'm making it clearer here :
好的,我想对这一点有点怀疑,所以也许我在这里更清楚:
When jquery doc says : "We can stop the loop from within the callback function by returning false." and you do :
Function () { for() { if found { return true; }
} return false; }
当 jquery doc 说:“我们可以通过返回 false 从回调函数中停止循环。” 你也是 :
函数 () { for() { 如果找到 { return true; }
}返回false; }
This doesn't mean that you're function will return true when find the searched element. Instead, it will always return false.
这并不意味着您的函数在找到搜索到的元素时会返回 true。相反,它将始终返回 false。
So to make your function work as you whish I propose to do so :
因此,为了使您的功能按您的意愿工作,我建议这样做:
Function () {
variable found = false;
foreach() {
if found {
found = true;
return false; // This statement doesn't make your function return false but just cut the loop
}
}
return found;
}
}
Of course there are many other ways to perform this but I think this is the simplest one.
当然还有很多其他方法可以执行此操作,但我认为这是最简单的一种。
Coopa - Easy !
Coopa - 简单!
回答by One_Dead_Headphone
As others have noted from jQuery Each, returning false will only break from the loop not return the value, returning true however will 'continue' and immediately begin the next iteration. With that knowledge, you could somewhat simplify your code like this:
正如其他人从jQuery Each 中指出的那样,返回 false 只会从循环中中断而不返回值,返回 true 但将“继续”并立即开始下一次迭代。有了这些知识,您可以像这样简化代码:
function HasStores(state) {
var statehasstores = false;
$(stores).each(function (index, store){
// continue or break;
statehasstores = !(state == store.state && store.category == "meyers"))
return statehasstores;
});
return !statehasstores;
}
This of course is a little silly using the double negative, and has the side effect of saving 'true' to statehasstores for every false iteration and vice versa, however the end result should be the same and you no longer have that if statement.
这当然是使用双重否定有点愚蠢,并且具有将“true”保存到 statehasstores 为每次错误迭代的副作用,反之亦然,但是最终结果应该是相同的,并且您不再有 if 语句。