Created
March 20, 2016 09:57
-
-
Save kindaro/6b164977acda2211914d to your computer and use it in GitHub Desktop.
Object.deep_extract (obj, props)
This file contains 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.deep_extract = | |
(obj, props) -> | |
if props.length == 0 | |
obj | |
else | |
prop = props[0] | |
if obj[prop]? then Object.deep_extract obj[prop], props[1..] else undefined | |
test_object = | |
1: | |
'key': 'value' | |
2: 'b' | |
3: 'c' | |
console.log Object.deep_extract test_object , ['1', 'key'] | |
console.log Object.deep_extract test_object , ['some', 'other', 'key'] | |
# Output: | |
# > value | |
# > undefined |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment