Explorer
Node.js

Write a function `getIn(obj, path, defaultVal)` that safely accesses a deeply nested property using a dot-notation path. Returns defaultVal if not found.

Problem Statement

Write a function `getIn(obj, path, defaultVal)` that safely accesses a deeply nested property using a dot-notation path. Returns defaultVal if not found.

Complexity

Time Complexity: -

Space Complexity: -

Hints

šŸ’” Hint 1: Split the path by "." and traverse the object. šŸ’” Hint 2: If any intermediate value is null/undefined, return the default. šŸ’” Hint 3: Use reduce to walk through the path segments. āœ… Solution: ```javascript function getIn(obj, path, defaultVal) { const result = path.split(".").reduce((acc, key) => acc != null ? acc[key] : undefined, obj); return result !== undefined ? result : defaultVal; } ```

getIn - Safe Deep Property Access

Hard
Write a function `getIn(obj, path, defaultVal)` that safely accesses a deeply nested property using a dot-notation path. Returns defaultVal if not found.
Editor
Loading Editor...
Evaluate code
Output:Click "Run" above to execute and verify your code here.
[42,"default","missing"]