Skip to content

Instantly share code, notes, and snippets.

@sagiavinash
Created April 28, 2015 17:08
Show Gist options
  • Save sagiavinash/c50cc42fc519b77aa37c to your computer and use it in GitHub Desktop.
Save sagiavinash/c50cc42fc519b77aa37c to your computer and use it in GitHub Desktop.
Method to get value of nth level enumerable property of an object.
Object.defineProperty(Object.prototype, 'deepVal', {
enumerable: false,
configurable: false,
writable: false,
value: function (string){
var props = string.split("."),
val = {};
for(var key in this){
val[key] = this[key];
}
for(var i = 0,len = props.length;i<len;i++){
val = val[props[i]];
}
return val;
}
});
var a = {
"b" : {
"c" : "d"
}
};
console.log(a.deepVal("b.c")); // d
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment