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.00
# lodash.get
1
2
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.
3
4
## Package Information
5
6
- **Package Name**: lodash.get
7
- **Package Type**: npm
8
- **Language**: JavaScript
9
- **Installation**: `npm install lodash.get`
10
11
## Core Imports
12
13
```javascript
14
const get = require('lodash.get');
15
```
16
17
For ES modules:
18
19
```javascript
20
import get from 'lodash.get';
21
```
22
23
## Basic Usage
24
25
```javascript
26
const get = require('lodash.get');
27
28
// Simple object property access
29
const user = { profile: { name: 'Alice', age: 30 } };
30
const userName = get(user, 'profile.name');
31
// Result: 'Alice'
32
33
// Array index access
34
const users = [{ name: 'Alice' }, { name: 'Bob' }];
35
const firstUserName = get(users, '[0].name');
36
// Result: 'Alice'
37
38
// With default value for missing properties
39
const email = get(user, 'profile.email', 'no-email@example.com');
40
// Result: 'no-email@example.com'
41
42
// Safe access - no errors on null/undefined
43
const result = get(null, 'any.path', 'default');
44
// Result: 'default'
45
```
46
47
## Capabilities
48
49
### Safe Property Access
50
51
Gets the value at `path` of `object`. If the resolved value is `undefined`, the `defaultValue` is returned in its place.
52
53
```javascript { .api }
54
/**
55
* Gets the value at `path` of `object`. If the resolved value is
56
* `undefined`, the `defaultValue` is returned in its place.
57
*
58
* @param {Object} object The object to query.
59
* @param {Array|string} path The path of the property to get.
60
* @param {*} [defaultValue] The value returned for `undefined` resolved values.
61
* @returns {*} Returns the resolved value.
62
*/
63
function get(object, path, defaultValue);
64
```
65
66
**Path Format Support:**
67
68
The `path` parameter supports multiple formats for flexible property access:
69
70
- **Dot notation**: `'a.b.c'` for nested object properties
71
- **Bracket notation**: `'a[0].b.c'` for array indices and property names
72
- **Array notation**: `['a', 'b', 'c']` for programmatic path construction
73
- **Mixed notation**: `'a[0].b["key"].c'` combining different formats
74
- **Symbol keys**: Supports symbol property keys
75
- **Numeric indices**: Array and object numeric property access
76
77
**Usage Examples:**
78
79
```javascript
80
const get = require('lodash.get');
81
82
// Dot notation for nested objects
83
const data = { user: { profile: { name: 'Alice' } } };
84
get(data, 'user.profile.name'); // 'Alice'
85
86
// Bracket notation for arrays
87
const users = [{ id: 1, name: 'Alice' }, { id: 2, name: 'Bob' }];
88
get(users, '[0].name'); // 'Alice'
89
get(users, '0.name'); // 'Alice' (equivalent)
90
91
// Array path notation
92
get(data, ['user', 'profile', 'name']); // 'Alice'
93
94
// Mixed notation
95
const complex = {
96
items: [
97
{ 'special-key': { value: 42 } }
98
]
99
};
100
get(complex, 'items[0]["special-key"].value'); // 42
101
102
// With default values
103
get(data, 'user.profile.email', 'no-email'); // 'no-email'
104
get(null, 'any.path', 'default'); // 'default'
105
get(undefined, 'any.path', 'default'); // 'default'
106
107
// Symbol keys
108
const sym = Symbol('key');
109
const obj = { [sym]: { nested: 'value' } };
110
get(obj, [sym, 'nested']); // 'value'
111
```
112
113
**Error Handling:**
114
115
The function provides robust error handling for common edge cases:
116
117
- Returns `undefined` when `object` is `null` or `undefined`
118
- Returns `defaultValue` when the path resolves to `undefined`
119
- Never throws errors for invalid paths or missing properties
120
- Handles circular references and complex object structures safely
121
- Preserves special numeric values like `-0` and `NaN`
122
123
**Type Support:**
124
125
Works with all JavaScript data types and structures:
126
127
- Objects (including nested objects)
128
- Arrays (including multi-dimensional arrays)
129
- Primitives (when used as objects)
130
- Functions (as objects with properties)
131
- Symbol keys and values
132
- Special values (`null`, `undefined`, `NaN`, `Infinity`)
133
134
## Types
135
136
Since this is a JavaScript library without TypeScript definitions, parameter types are specified using JSDoc notation:
137
138
```javascript { .api }
139
/**
140
* @typedef {Object|Array|null|undefined} ObjectLike
141
* Any value that can have properties accessed
142
*/
143
144
/**
145
* @typedef {string|Array<string|number|symbol>} PropertyPath
146
* A property path as string (dot/bracket notation) or array of keys
147
*/
148
149
/**
150
* Main get function type signature
151
* @function get
152
* @param {ObjectLike} object - The object to query
153
* @param {PropertyPath} path - The path of the property to get
154
* @param {*} [defaultValue] - The value returned for undefined resolved values
155
* @returns {*} Returns the resolved value or defaultValue
156
*/
157
```