2

I've got a json array and a simple function to return a bit of data. I can log the bit of data I want to the console (the function currently does this for testing), but it's not returning.

Stackers, please help my worn out brain and let me know where I've goofed.

(code is super self explanatory, function call is at the bottom of the .js)

http://jsfiddle.net/BDJeU/

function getCountryData(data, country)
{
    $.each(data, function(index) {

        if( data[index]["Country"] == country )
        {
            console.log( data[index]["Country"] );
            console.log( data[index]["data"] );
            return data[index]["data"];
        }
    });
}

5 Answers 5

4

Try this:

http://jsfiddle.net/BDJeU/3/

    function getCountryData(data, country)
    {
        var returnData;
        $.each(data, function(index) {

            if( data[index]["Country"] == country )
            {
                returnData = data[index]["data"];
                return false;
            }
        });
        return returnData;
    }

The reason the it wasn't working was because you were returning to the each function. So setting a variable that gets assigned the value outside of the iteration will get you the data you want.

Sign up to request clarification or add additional context in comments.

1 Comment

@Greg-J No worries. I have definitely had those moments where the answer is starring me in the face.
1

Your return is inside the anonymous function used by each. So getCountryData itself has no return (so defaults to returning undefined). It needs to be something like this:

function getCountryData(data, country)
{
    var result;
    $.each(data, function(index) {

        if( data[index]["Country"] == country )
        {
            console.log( data[index]["Country"] );
            console.log( data[index]["data"] );
            result = data[index]["data"];
        }
    });

    return result;
}

Comments

1

$.grep() could be better in this case: http://jsfiddle.net/zerkms/BDJeU/12/

function getCountryData(data, country)
{
    var result = $.grep(data, function(item) {
        return item["Country"] == country;
    });

    return result && result[0] && result[0]['data'];
}

1 Comment

Interesting, I hadn't seen this function in jQuery before this. Good answer.
1

You need to return outside each and use return to break early out of the each iteration:

function getCountryData(data, country)
    {
        var res;
        $.each(data, function(index) {

            if( data[index]["Country"] == country )
            {
                res = data[index]["data"];

                // once found, stop searching and
                // break early out of the each iteration                  
                return; 
            }
        });
        return res;
    }

Comments

1

Should you really be using each? if you are trying to filter the results to a single answer try

function getCountryData(data, country) {
    var matchingCountries = $.grep(data, function(row){
        return row.Country == country;
    });
    if (matchingCountries.length > 0)
        return matchingCountries[0].data;
}

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.