Created
January 22, 2020 03:17
-
-
Save gregzanch/38481dbe34bc4aaf6d40e627741f6ee2 to your computer and use it in GitHub Desktop.
Get an object property via a path like string
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
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