admin管理员组文章数量:1277317
I can't figure out why this is happening.
The following function always returns undefined. Even when the condition is satisfied and a value should be returned.
Here is an instance of the answerCollection variable.
[
Object
Answer: "2"
AnswerText: undefined
OpsID: "24"
PprID: "2"
Question: "How many colors?"
__proto__: Object
]
.
function GetAnswerForProcessQuestion(pprID)
{
$.each(answerCollection, function (index, item)
{
var thisPprID = item["PprID"];
if (thisPprID == pprID)
{
var answer = item["Answer"];
return answer;
}
});
}
However, if I set a variable inside the loop, then return that variable once the loop finishes executing, the correct value is returned.
function GetAnswerForProcessQuestion(pprID)
{
var answer;
$.each(answerCollection, function (index, item)
{
var thisPprID = item["PprID"];
if (thisPprID == pprID)
{
answer = item["Answer"];
}
});
return answer;
}
Any ideas on why I can't return a value from inside the loop?
I can't figure out why this is happening.
The following function always returns undefined. Even when the condition is satisfied and a value should be returned.
Here is an instance of the answerCollection variable.
[
Object
Answer: "2"
AnswerText: undefined
OpsID: "24"
PprID: "2"
Question: "How many colors?"
__proto__: Object
]
.
function GetAnswerForProcessQuestion(pprID)
{
$.each(answerCollection, function (index, item)
{
var thisPprID = item["PprID"];
if (thisPprID == pprID)
{
var answer = item["Answer"];
return answer;
}
});
}
However, if I set a variable inside the loop, then return that variable once the loop finishes executing, the correct value is returned.
function GetAnswerForProcessQuestion(pprID)
{
var answer;
$.each(answerCollection, function (index, item)
{
var thisPprID = item["PprID"];
if (thisPprID == pprID)
{
answer = item["Answer"];
}
});
return answer;
}
Any ideas on why I can't return a value from inside the loop?
Share Improve this question edited Aug 8, 2012 at 14:54 gen_Eric 227k42 gold badges303 silver badges342 bronze badges asked Aug 8, 2012 at 14:48 KevinKevin 1,9503 gold badges25 silver badges38 bronze badges8 Answers
Reset to default 5Returning a value from $.each
does not return the value from the parent function. Try doing it this way:
function GetAnswerForProcessQuestion(pprID)
{
var answer;
$.each(answerCollection, function (index, item)
{
var thisPprID = item["PprID"];
if (thisPprID == pprID)
{
answer = item["Answer"];
return false; // break loop
}
});
return answer;
}
well simply because the return value is outside or inside of the running function. i suggest you use the java-script default for-each code:
function GetAnswerForProcessQuestion(pprID)
{
for (var i in answerCollection){ // note that answerCollection must be defined
// globaly or passed in
var thisPprID = answerCollection[i]["PprID"];
if (thisPprID == pprID) //checking if it's the value
{
return answerCollection[i]["Answer"]; //if the condition is true return it
}
}
return false; // if you find nothing you end up here
}
the variable defined inside the function will be accesable to any other function inside that function but not outside. meaning:
function somefunction(){
var test='the value';
function testing(){ alert(test); } //will alert 'the value' since the variable was
testing(); //defined in the functions scope
function testing(){
var test=1;//the test ins now defined under the function testing()
}
alert(test); //will alert 'undefined' since it was not defined in the global scope
}
The .each in jquery does not return a value returned from the function that you call. It responds to return true;
and return false;
as a way to simulate continue;
and break;
respectively. Also, .each never returns a value. Therefore returning anything else from your called function will never be passed on.
The reason that your second form works is due to the way variables scope. Since the variable is declared in a parent of the function being wrapped by the .each call, it is accessible inside the function and can be set. The fact that it's scope belongs to the parent allows it to be read and returned in that parent.
You are returning from the inner function, not the outer one. That is why the outer function returns underfined
as this is the default.
You can use return false;
to end the each loop immediately and then return the result.
function GetAnswerForProcessQuestion(pprID)
{
var answer;
$.each(answerCollection, function (index, item)
{
var thisPprID = item["PprID"];
if (thisPprID == pprID)
{
answer = item["Answer"];
return false;
}
});
return answer;
}
http://jsfiddle/mN5B3/
$.each is its own function, notice the $.each(answerCollection, function (index, item)
You're returning from that, not the parents.
The loop has an anonymous function. When you return, you return from that inner function and the loop continues.
You should set the variable and then return false from your inner function (this will break the each loop), then return the variable from the outer function.
Your callback within the $.each
in your code is defined as follows:
function callback(index, item)
{
var thisPprID = item["PprID"];
if (thisPprID == pprID)
{
var answer = item["Answer"];
return answer;
}
}
Which is in the function:
function GetAnswerForProcessQuestion(pprID)
{
$.each(answerCollection, callback);
}
Which means that in the top function, nothing happens! For each of the items, you calculate something, and it stays within the loop.
In your second code, you save the answer to the scope of the outer function. That way, the outer function can return it.
From the jQuery .each() docs:
We can break the $.each() loop at a particular iteration by making the callback function return false. Returning non-false is the same as a continue statement in a for loop; it will skip immediately to the next iteration.
本文标签: javascriptWhy is function return value undefined when returned in a loopStack Overflow
版权声明:本文标题:javascript - Why is function return value undefined when returned in a loop? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741226163a2361898.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论