Hack Frontend Community

Get Value from Object by Path

Ozon
Write a function get(obj, path) that takes an object obj and a string path representing the path to a value in the object.
  • - If the path exists, the function should return the corresponding value.
  • - If the path does not exist, the function should return undefined.
  • - The path is passed as a string separated by dots (.).

Examples:

Input 1: { a: { b: { c: "d" } } }, "a.b.c"
Output 1: "d"
Input 2: { a: { b: { c: "d" } } }, "a.b"
Output 2: { c: "d" }
Input 3: { x: { y: { z: 42 } } }, "x.y.a"
Output 3: undefined
Run your code to see results.