0

I'm struggling to get one of my JavaScript functions return its value. Please observe following code:

function GW2API_getEventInfo(p_eventid) {
    console.log("Getting EventInfo for event " + p_eventid);
    $.each(arrEvents, function(i, eventItem) {
        $.each(eventItem, function(j, eventInfo) {
            if (eventInfo.event_id == p_eventid) {
                console.log(GW2API_getEventName(p_eventid) + " - " + eventInfo.state);
                return {
                    'name': GW2API_getEventName(p_eventid),
                    'state': eventInfo.state
                };
            }
        });
    });
}

Now I would like to return an object with two properties: 'name' and 'state'. However, no matter what I add underneath the console.log, my function isn't returning anything (although data is found, hence the console is logging).

currEvent = GW2API_getEventInfo(GW2API_events_ShadowBehemoth[i]); alert(currEvent) --> yields "undefined"

4
  • Look at this answer: stackoverflow.com/a/14441307/1233508 . You need to create a variable at the top level of the function, assign the value you want returned to it, and then return it after the loops are done. Commented Jun 14, 2013 at 14:19
  • Where do you expect it to return to? It's an anonymous function that is being passed as an argument to a jQuery function. That function is returning your object, it's just not being returned to your code. Commented Jun 14, 2013 at 14:19
  • Thanks all for your replies here. Indeed, very silly mistake of mine, not returning the return value from the inner to the outer loop.. After declaring a variable on top level of the function and simply assigning data to it in the loop (and returning the filled variable after the loops) works like a charm!! Commented Jun 14, 2013 at 14:26
  • @Tribio - to be honest, you're over-complicating it by using jQuery .each() instead of a standard for() loop. That's the root of the problem here. Commented Jun 14, 2013 at 14:39

2 Answers 2

1

It should - you're not returning anything.

You're returning something inside the anonymous function of the second $.each, but you're not returning anything in the GW2API_getEventInfo function.

Maybe this is what you meant:

function GW2API_getEventInfo(p_eventid) {
    var result = null;
    console.log("Getting EventInfo for event " + p_eventid);
    $.each(arrEvents, function(i, eventItem) {
        $.each(eventItem, function(j, eventInfo) {
            if (eventInfo.event_id == p_eventid) {
                console.log(GW2API_getEventName(p_eventid) + " - " + eventInfo.state);
                result = {
                    'name': GW2API_getEventName(p_eventid),
                    'state': eventInfo.state
                };
                return; //Out of the inner $.each
            }
        });
        if (result !== null) {
            return; //Out of the outer $.each
        }
    });
    return result; //Return the value in the GW2API_getEventInfo function
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you for the code snippet! Indeed, not returning the data from the inner to the outer and that data from the outer to the rest of the code, I kinda feel like a newbie now.. Thanks again!
0

Using .each() means that you're creating an inner closure function. As things stand, you're returning from the closure function, but not returning from your main function

You would probably be better off not using jQuery .each() here at all, and just using a standard Javascript for() loop.

function GW2API_getEventInfo(p_eventid) {
    for(var i=0; i<=arrEvents.length; i++) {
        var eventItem = arrEvents[i];
        for(var j=0; j<=eventItem.length; j++) {
            var eventInfo = eventItem[j];
            if (eventInfo.event_id == p_eventid) {
                return {
                    'name': GW2API_getEventName(p_eventid),
                    'state': eventInfo.state
                };
            }
        });
    });
}

This avoids the need to use closure functions, which avoids the confusion over return values.

It is also likely to run quicker than the jQuery version.

(note, I don't know your data structure here; I've assumed that it's arrEvents and eventItem are both arrays. If they're objects, you will need to use a for(..in..) style loop instead, but the principle is the same)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.