Skip to content

Instantly share code, notes, and snippets.

@jimjeffers
Last active July 22, 2017 15:47
Show Gist options
  • Save jimjeffers/23f8a82cfdda1744d31f656d40e9ba73 to your computer and use it in GitHub Desktop.
Save jimjeffers/23f8a82cfdda1744d31f656d40e9ba73 to your computer and use it in GitHub Desktop.
Naive object look up.
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