The lodash method `_.result` exported as a module for getting values at object paths with function resolution and default value support.
npx @tessl/cli install tessl/npm-lodash--result@4.5.00
# lodash.result
1
2
lodash.result provides the `_.result` utility function from the lodash library as a standalone Node.js module. It enables deep property access with support for resolving functions by calling them if the retrieved value is a function, and providing a default value if the path doesn't exist or resolves to undefined.
3
4
## Package Information
5
6
- **Package Name**: lodash.result
7
- **Package Type**: npm
8
- **Language**: JavaScript
9
- **Installation**: `npm install lodash.result`
10
11
## Core Imports
12
13
```javascript
14
var result = require('lodash.result');
15
```
16
17
## Basic Usage
18
19
```javascript
20
var result = require('lodash.result');
21
22
var object = {
23
'user': {
24
'name': 'Alice',
25
'getAge': function() { return 30; },
26
'details': {
27
'address': '123 Main St'
28
}
29
}
30
};
31
32
// Get a simple property
33
result(object, 'user.name');
34
// => 'Alice'
35
36
// Get a nested property
37
result(object, 'user.details.address');
38
// => '123 Main St'
39
40
// Function resolution - automatically calls functions
41
result(object, 'user.getAge');
42
// => 30
43
44
// With default value for missing paths
45
result(object, 'user.email', 'no-email@example.com');
46
// => 'no-email@example.com'
47
48
// Array notation for paths
49
result(object, ['user', 'name']);
50
// => 'Alice'
51
```
52
53
## Capabilities
54
55
### Property Path Resolution
56
57
Gets the value at a specified path of an object, with support for resolving functions by calling them and providing default values for missing paths.
58
59
```javascript { .api }
60
/**
61
* Gets the value at path of object. If the path doesn't exist or resolves to undefined,
62
* the defaultValue is returned. If the resolved value is a function, it's invoked with
63
* the this binding of its parent object and its result is returned.
64
*
65
* @param {Object} object - The object to query
66
* @param {Array|string} path - The path of the property to resolve
67
* @param {*} [defaultValue] - The value returned if the resolved value is undefined
68
* @returns {*} Returns the resolved value
69
*/
70
function result(object, path, defaultValue);
71
```
72
73
**Parameters:**
74
75
- **object** (`Object`): The object to query
76
- **path** (`Array|string`): The path of the property to resolve. Can be:
77
- String notation: `'a.b.c'` or `'a[0].b.c'`
78
- Array notation: `['a', 'b', 'c']` or `['a', 0, 'b', 'c']`
79
- **defaultValue** (`*`, optional): The value returned if the resolved value is `undefined`
80
81
**Returns:**
82
83
- (`*`): Returns the resolved value, or the result of calling a function if the value at path is a function, or the defaultValue if path doesn't exist
84
85
**Key Behavior:**
86
87
1. **Path Resolution**: Supports both string (`'a.b.c'`) and array (`['a', 'b', 'c']`) path notation
88
2. **Array Index Access**: Handles array indices in both string (`'arr[0].prop'`) and array (`['arr', 0, 'prop']`) formats
89
3. **Function Resolution**: If the value at the specified path is a function, automatically calls it with the parent object as context (`this`)
90
4. **Default Value Handling**: Returns the provided `defaultValue` if the path doesn't exist or resolves to `undefined`
91
5. **Null/Undefined Safety**: Safely handles `null` or `undefined` objects without throwing errors
92
93
**Usage Examples:**
94
95
```javascript
96
var result = require('lodash.result');
97
98
// Complex nested object with functions
99
var userData = {
100
profile: {
101
name: 'John Doe',
102
getFullName: function() {
103
return this.name + ' (verified)';
104
},
105
settings: {
106
theme: 'dark',
107
notifications: {
108
email: true,
109
push: false
110
}
111
}
112
},
113
posts: [
114
{ title: 'First Post', views: 100 },
115
{ title: 'Second Post', views: 250 }
116
]
117
};
118
119
// String path notation
120
result(userData, 'profile.name');
121
// => 'John Doe'
122
123
// Function resolution with context
124
result(userData, 'profile.getFullName');
125
// => 'John Doe (verified)'
126
127
// Deep nested property access
128
result(userData, 'profile.settings.notifications.email');
129
// => true
130
131
// Array index access
132
result(userData, 'posts[0].title');
133
// => 'First Post'
134
135
// Array notation for complex paths
136
result(userData, ['posts', 1, 'views']);
137
// => 250
138
139
// Default value for missing paths
140
result(userData, 'profile.age', 'Unknown');
141
// => 'Unknown'
142
143
result(userData, 'nonexistent.deeply.nested.path', 'Not found');
144
// => 'Not found'
145
146
// Safe access on null/undefined objects
147
result(null, 'any.path', 'Safe default');
148
// => 'Safe default'
149
150
result(undefined, 'any.path', 'Safe default');
151
// => 'Safe default'
152
```
153
154
**Common Use Cases:**
155
156
- **Safe Property Access**: Accessing nested object properties without risking `TypeError` on undefined intermediate values
157
- **Configuration Objects**: Retrieving configuration values with fallback defaults
158
- **API Response Processing**: Safely extracting data from potentially incomplete API responses
159
- **Dynamic Property Access**: Using computed property paths stored as strings or arrays
160
- **Function Resolution**: Automatically calling getter methods or computed properties