JavaScript Utilities
Safe Object Property Access (get)
easyJS SpecificJS / TS8 tests
Problem Statement
Implement a helper function `get(object, path, defaultValue)` that safely retrieves the value at a nested `path` of an object, similar to Lodash `_.get`. Requirements: 1. `path` can be a dot-separated string (e.g. `"a.b.c"`) or an array of string keys (e.g. `["a", "b", "c"]`). 2. Array indices can also be accessed with dot notation (e.g. `"items.0.name"`). 3. If the resolved value at the path is `undefined` or null-safety is broken along the traversal path, return `defaultValue`. 4. If `defaultValue` is not provided, return `undefined`.
Examples
Example 1:
Input: object = { a: { b: { c: 3 } } }, path = "a.b.c"
Output: 3
Example 2:
Input: object = { users: [{ name: "Alice" }] }, path = "users.0.name"
Output: "Alice"
Example 3:
Input: object = { a: null }, path = "a.b.c", defaultValue = "fallback"
Output: "fallback"JavaScript & TypeScript starter code included
Run this as a live interviewPractice solo
8 test cases. No second person needed.