L.Clark L.Clark - 4 months ago 72
Javascript Question

jQuery.Deferred exception: Cannot read property

var arr = [{
value: 'a'
}];
var getTest = function() {
jQuery.each(arr, function(i, val) {

if (val.value == "a") {
return val;
}
});
}

alert(getTest().value);



jquery-3.1.0.js:3793 Uncaught TypeError: Cannot read property 'value' of undefined

Answer
var arr = [{
    value: 'a'
}];
var getTest = function() {
    var toRet;
    jQuery.each(arr, function(i, val) {
        if (val.value == "a") {
            toRet = val;
        }
    });
    return toRet;
}

alert(getTest().value);

JQueary.each is actually a function and you are returning value there and not to your function getTest(). This is working solution.

Comments