CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/npm-lodash-get

The lodash method _.get exported as a module for safely accessing nested object properties using path notation.

Pending
Quality

Pending

Does it follow best practices?

Impact

Pending

No eval scenarios have been run

SecuritybySnyk

Pending

The risk profile of this skill

Overview
Eval results
Files

lodash.get

The 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.

Package Information

  • Package Name: lodash.get
  • Package Type: npm
  • Language: JavaScript
  • Installation: npm install lodash.get

Core Imports

const get = require('lodash.get');

For ES modules:

import get from 'lodash.get';

Basic Usage

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'

Capabilities

Safe Property Access

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:

  • Dot notation: 'a.b.c' for nested object properties
  • Bracket notation: 'a[0].b.c' for array indices and property names
  • Array notation: ['a', 'b', 'c'] for programmatic path construction
  • Mixed notation: 'a[0].b["key"].c' combining different formats
  • Symbol keys: Supports symbol property keys
  • Numeric indices: Array and object numeric property access

Usage 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:

  • Returns undefined when object is null or undefined
  • Returns defaultValue when the path resolves to undefined
  • Never throws errors for invalid paths or missing properties
  • Handles circular references and complex object structures safely
  • Preserves special numeric values like -0 and NaN

Type Support:

Works with all JavaScript data types and structures:

  • Objects (including nested objects)
  • Arrays (including multi-dimensional arrays)
  • Primitives (when used as objects)
  • Functions (as objects with properties)
  • Symbol keys and values
  • Special values (null, undefined, NaN, Infinity)

Types

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
 */
Workspace
tessl
Visibility
Public
Created
Last updated
Describes
npmpkg:npm/lodash.get@4.4.x
Publish Source
CLI
Badge
tessl/npm-lodash-get badge