Sobes.tech
Junior — Senior

Getting the value of a nested property by string path

livecode

Task condition

It is necessary to write a function that extracts a value from a nested object using a path represented by a dot-separated string. If the specified path does not exist in the object, the function should return undefined.

<script>
function getProp(obj, path) {
  //code...
}

const obj = {
  a: {
    b: 10,
    c: {
      d: {}
    }
  }
};

getProp(obj, 'a.b');        // => 10
getProp(obj, 'a.c.d');      // => {}
getProp(obj, 'a.c.z.f.s');  // => undefined
</script>