The lodash method _.get exported as a module for safely accessing nested object properties using path notation.
npx @tessl/cli install tessl/npm-lodash--get@4.4.0The lodash method _.get exported as a Node.js module. Provides safe property access for nested objects using path notation, with support for default values when properties are undefined or missing.
npm install lodash.getconst get = require('lodash.get');For ES modules:
import get from 'lodash.get';const get = require('lodash.get');
// Simple object property access
const user = { profile: { name: 'Alice', age: 30 } };
const userName = get(user, 'profile.name');
// Result: 'Alice'
// Array index access
const users = [{ name: 'Alice' }, { name: 'Bob' }];
const firstUserName = get(users, '[0].name');
// Result: 'Alice'
// With default value for missing properties
const email = get(user, 'profile.email', 'no-email@example.com');
// Result: 'no-email@example.com'
// Safe access - no errors on null/undefined
const result = get(null, 'any.path', 'default');
// Result: 'default'Gets the value at path of object. If the resolved value is undefined, the defaultValue is returned in its place.
/**
* Gets the value at `path` of `object`. If the resolved value is
* `undefined`, the `defaultValue` is returned in its place.
*
* @param {Object} object The object to query.
* @param {Array|string} path The path of the property to get.
* @param {*} [defaultValue] The value returned for `undefined` resolved values.
* @returns {*} Returns the resolved value.
*/
function get(object, path, defaultValue);Path Format Support:
The path parameter supports multiple formats for flexible property access:
'a.b.c' for nested object properties'a[0].b.c' for array indices and property names['a', 'b', 'c'] for programmatic path construction'a[0].b["key"].c' combining different formatsUsage Examples:
const get = require('lodash.get');
// Dot notation for nested objects
const data = { user: { profile: { name: 'Alice' } } };
get(data, 'user.profile.name'); // 'Alice'
// Bracket notation for arrays
const users = [{ id: 1, name: 'Alice' }, { id: 2, name: 'Bob' }];
get(users, '[0].name'); // 'Alice'
get(users, '0.name'); // 'Alice' (equivalent)
// Array path notation
get(data, ['user', 'profile', 'name']); // 'Alice'
// Mixed notation
const complex = {
items: [
{ 'special-key': { value: 42 } }
]
};
get(complex, 'items[0]["special-key"].value'); // 42
// With default values
get(data, 'user.profile.email', 'no-email'); // 'no-email'
get(null, 'any.path', 'default'); // 'default'
get(undefined, 'any.path', 'default'); // 'default'
// Symbol keys
const sym = Symbol('key');
const obj = { [sym]: { nested: 'value' } };
get(obj, [sym, 'nested']); // 'value'Error Handling:
The function provides robust error handling for common edge cases:
undefined when object is null or undefineddefaultValue when the path resolves to undefined-0 and NaNType Support:
Works with all JavaScript data types and structures:
null, undefined, NaN, Infinity)Since this is a JavaScript library without TypeScript definitions, parameter types are specified using JSDoc notation:
/**
* @typedef {Object|Array|null|undefined} ObjectLike
* Any value that can have properties accessed
*/
/**
* @typedef {string|Array<string|number|symbol>} PropertyPath
* A property path as string (dot/bracket notation) or array of keys
*/
/**
* Main get function type signature
* @function get
* @param {ObjectLike} object - The object to query
* @param {PropertyPath} path - The path of the property to get
* @param {*} [defaultValue] - The value returned for undefined resolved values
* @returns {*} Returns the resolved value or defaultValue
*/