3

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?

gen_Eric
  • 223,194
  • 41
  • 299
  • 337
Kevin
  • 1,940
  • 3
  • 24
  • 38

8 Answers8

5

Returning 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;
    }
Kevin B
  • 94,570
  • 16
  • 163
  • 180
2

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

}

volchkov
  • 69
  • 7
1

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.

JamieSee
  • 12,696
  • 2
  • 31
  • 47
0

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.net/mN5B3/

Richard Dalton
  • 35,513
  • 6
  • 73
  • 91
  • The .each in jquery does not respond to breaks or continuations inside the called function. If you want to simulate a `break;` use `return false;`. To simulate a `continue;` use `return true;` – JamieSee Aug 08 '12 at 15:03
0

$.each is its own function, notice the $.each(answerCollection, function (index, item)

You're returning from that, not the parents.

Jure C.
  • 3,013
  • 28
  • 33
0

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.

gcochard
  • 11,408
  • 1
  • 26
  • 41
0

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.

0

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.

epascarello
  • 204,599
  • 20
  • 195
  • 236