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)
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"];
}
});
}
Try this:
http://jsfiddle.net/BDJeU/3/
The reason the it wasn’t working was because you were returning to the
eachfunction. So setting a variable that gets assigned the value outside of the iteration will get you the data you want.