Created
April 28, 2015 17:08
-
-
Save sagiavinash/c50cc42fc519b77aa37c to your computer and use it in GitHub Desktop.
Method to get value of nth level enumerable property of an object.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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