I'm pulling a blank as to why this code isn't working as expected (jsFiddle here):
data = [
{
"id": 1,
"value": 4.56
},
{
"id": 2,
"value": 7.89
}];
function FindMe(searchID)
{
$.each(data, function (i, v)
{
// i=index, v=value (which is an object)
if (v.id === searchID)
{
console.log("Found: ");
console.log(v);
return v; // pass the desired object back to caller
}
});
}
console.clear();
var test = FindMe(2); // causes the console to show the correct object
console.log("Returned: ");
console.log(test); // shows "undefined" instead of a returned object
The function clearly does its job to find the correct array element (the console shows this as "Found"), but the return isn't happening. What's going wrong here?