get()
get<
T>(object,path,defaultValue?):T|undefined
Gets the value at path of object with optional default.
Handles null/undefined objects and intermediate properties safely.
Type Parametersโ
T: T = anyโ
The type of the default value and return type.
Parametersโ
object: anyโ
The object to query.
path: string | (string | number | symbol)[]โ
Property path as dot notation string or array of keys (string, number, or symbol).
defaultValue?: Tโ
Value returned if resolved value is undefined.
Returns: T | undefinedโ
The resolved value or defaultValue.
See Alsoโ
Sinceโ
2.0.0
Noteโ
Supports bracket notation for arrays (e.g., 'items[0].name').
Noteโ
Blocks access to proto, constructor, and prototype for security.
Performanceโ
O(m) time where m is path length.
Also known asโ
get (Lodash, es-toolkit, Radashi, Effect) ยท path (Ramda) ยท pathOr (Remeda) ยท โ (Modern Dash, Antfu)
Exampleโ
const obj = { a: { b: { c: 3 } } };
get(obj, 'a.b.c'); // => 3
get(obj, 'a.b.d'); // => undefined
get(obj, 'a.b.d', 'default'); // => 'default'
get(obj, ['a', 'b', 'c']); // => 3
const data = { items: [{ name: 'first' }] };
get(data, 'items[0].name'); // => 'first'
get(null, 'any.path', 'safe'); // => 'safe'
get({}, '__proto__'); // => undefined
How it works?โ
Safely retrieves a value at a nested path with optional default.
Path Formatsโ
Default Valueโ
Null Safetyโ
Use Casesโ
Access nested data safely ๐โ
Retrieve a deeply nested value without throwing "cannot read property of undefined". Critical for accessing API response data.
// Safe access: returns undefined if path doesn't exist
const city = get(user, 'address.shipping.city');
Provide default fallbackโ
Return a default value if the resolved path is undefined or null.
Essential for UI rendering to prevent blank states.
const theme = get(config, 'ui.theme.color', 'blue');
Access array elementsโ
Retrieve items from nested arrays using dot notation. Useful for parsing JSON structures with lists.
const firstTag = get(post, 'tags[0].name');
Read i18n translation keysโ
Retrieve translation strings from deeply nested locale objects. Essential for internationalization systems with namespaced translation keys.
const translations = {
en: { auth: { login: { title: "Sign In", error: "Invalid credentials" } } },
fr: { auth: { login: { title: "Connexion", error: "Identifiants invalides" } } },
};
const t = (key: string, locale = "en") =>
get(translations, `${locale}.${key}`, key);
t("auth.login.title"); // => "Sign In"
t("auth.login.title", "fr"); // => "Connexion"
t("auth.signup.title"); // => "auth.signup.title" (fallback to key)
Extract data from webhook payloadsโ
Safely extract values from deeply nested third-party webhook payloads. Critical for Stripe, GitHub, Slack, and other webhook integrations where structure varies.
// Stripe webhook payload โ deeply nested
const stripeEvent = {
type: "checkout.session.completed",
data: {
object: {
customer_details: { email: "user@example.com", name: "Alice" },
amount_total: 4999,
payment_intent: { charges: { data: [{ receipt_url: "https://..." }] } },
},
},
};
const email = get(stripeEvent, "data.object.customer_details.email");
// => "user@example.com"
const receiptUrl = get(stripeEvent, "data.object.payment_intent.charges.data[0].receipt_url");
// => "https://..."
const refundReason = get(stripeEvent, "data.object.refund.reason", "none");
// => "none" (path doesn't exist, fallback used)