Skip to main content

Get Value from Object by Path

MIDDLE
SoftServe

Write a function getValueByPath(obj, path) that takes an object and a path string (e.g., "a.b.c") and returns the value at that path.

If the path doesn't exist, return undefined.

Example:

Input: obj = { a: { b: { c: 42 } } }, path = "a.b.c"
Output: 42

Examples:

Input 1:{"obj":{"a":{"b":{"c":42}}},"path":"a.b.c"}
Output 1:42
Input 2:{"obj":{"x":10},"path":"x"}
Output 2:10

Loading editor...

Run your code to see results

Click the Run button above