Aller au contenu principal

get()

get<T>(object, path, defaultValue?): T | undefined

Gets the value at path of object with optional default.

remarque

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

set


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)