getNestedValue
Retrieves a nested value from an object using dot notation path.
Safely traverses object properties using a dot-separated path string. Returns undefined if any part of the path is missing or if the traversal encounters a non-object value.
Presentation
function getNestedValue (obj: unknown, path: string): unknown;Returns
unknown -The value at the specified path, or undefined if not found
Parameters
| Name | Type | Description |
|---|---|---|
| obj | unknown | The object to traverse |
| path | string | Dot-separated path to the desired property |
Example usage
const data = {
user: {
profile: {
name: 'John',
age: 30
},
settings: { theme: 'dark' }
}
};
getNestedValue (data, 'user.profile.name') // 'John'
getNestedValue (data, 'user.settings.theme') // 'dark'
getNestedValue (data, 'user.profile.email') // undefined
getNestedValue (data, 'nonexistent.path') // undefined
getNestedValue (null, 'any.path') // undefined