L.Clark
L.Clark

Reputation: 71

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

Upvotes: 6

Views: 37409

Answers (1)

Aleksandar Đokić
Aleksandar Đokić

Reputation: 2351

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);

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

Upvotes: 4

Related Questions