Pokazywanie postów oznaczonych etykietą getProperty. Pokaż wszystkie posty
Pokazywanie postów oznaczonych etykietą getProperty. Pokaż wszystkie posty

poniedziałek, 30 czerwca 2014

get nested property of object

Function that allows to get nested property of object. If it is not defined, function will return undefined.

var getProperty = function(obj, propertySrc) {
    var src = propertySrc.split('.');
    var res = obj;

    for (var i = 0, l = src.length; i < l; ++i) {
        if (!(src[i] in res)) {
            return void(0);
        }
        res = res[src[i]];
    }
    return res;
};
Working example: http://jsfiddle.net/gkucmierz/5Df5D/