value at the path, or undefined if path doesn't exist
const user = {
profile: {
name: 'John',
settings: {
theme: 'dark',
notifications: true
}
}
};
reach(user, 'profile.name') // 'John'
reach(user, 'profile.settings.theme') // 'dark'
reach(user, 'profile.missing.property') // undefined
// Safe API response parsing
function getNestedValue(response: any, path: string) {
const value = reach(response, path);
return value ?? 'Not found';
}
const apiResponse = { data: { users: [{ name: 'Alice' }] } };
getNestedValue(apiResponse, 'data.users.0.name') // 'Alice'
getNestedValue(apiResponse, 'data.posts.0.title') // 'Not found'
// Working with Maps, Sets, and Arrays
const data = {
users: new Map([['john', { name: 'John', age: 30 }]]),
tags: new Set(['admin', 'user', 'moderator']),
scores: [100, 95, 87, 92]
};
reach(data, 'users.john.name') // 'John'
reach(data, 'tags.0') // 'admin'
reach(data, 'tags.admin') // 'admin' (if exists in Set)
reach(data, 'scores.1') // 95
reach(data, 'scores.5') // undefined
Reaches into an object, Map, Set, or Array and returns the value at the end of the path.
Safely navigates nested object properties using dot notation. Supports Maps (using .get() method), Sets (using numeric indices or .has()), and Arrays (using numeric indices). Returns undefined if any part of the path doesn't exist.