Last active
July 22, 2017 15:47
-
-
Save jimjeffers/23f8a82cfdda1744d31f656d40e9ba73 to your computer and use it in GitHub Desktop.
Naive object look up.
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
const get = (target, path) => path.split(".").reduce((value, key) => value ? value[key] : value, target) | |
const a = { | |
b: { | |
c: { | |
d: "result!" | |
} | |
} | |
} | |
const res = get(a, "b.c.d") | |
console.log(res) // "result!" | |
const nope = get(a, "b.z.d") | |
console.log(nope) // "undefined" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment