Skip to content

Instantly share code, notes, and snippets.

@gregzanch
Created January 22, 2020 03:17
Show Gist options
  • Save gregzanch/38481dbe34bc4aaf6d40e627741f6ee2 to your computer and use it in GitHub Desktop.
Save gregzanch/38481dbe34bc4aaf6d40e627741f6ee2 to your computer and use it in GitHub Desktop.
Get an object property via a path like string
const obj = {
name: "obj",
userData: {
position: {
x: 3,
y: 4,
z: 10
}
}
}
const at = (obj, path) => {
let keys = path.split(".");
if (keys.length > 1) {
return at(obj[keys[0]], keys.slice(1).join("."))
}
return obj[keys[0]];
}
at(obj,"userData.position.x") //=>3
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment