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.0lodash.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.
npm install lodash.resultvar result = require('lodash.result');var result = require('lodash.result');
var object = {
'user': {
'name': 'Alice',
'getAge': function() { return 30; },
'details': {
'address': '123 Main St'
}
}
};
// Get a simple property
result(object, 'user.name');
// => 'Alice'
// Get a nested property
result(object, 'user.details.address');
// => '123 Main St'
// Function resolution - automatically calls functions
result(object, 'user.getAge');
// => 30
// With default value for missing paths
result(object, 'user.email', 'no-email@example.com');
// => 'no-email@example.com'
// Array notation for paths
result(object, ['user', 'name']);
// => 'Alice'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.
/**
* Gets the value at path of object. If the path doesn't exist or resolves to undefined,
* the defaultValue is returned. If the resolved value is a function, it's invoked with
* the this binding of its parent object and its result is returned.
*
* @param {Object} object - The object to query
* @param {Array|string} path - The path of the property to resolve
* @param {*} [defaultValue] - The value returned if the resolved value is undefined
* @returns {*} Returns the resolved value
*/
function result(object, path, defaultValue);Parameters:
Object): The object to queryArray|string): The path of the property to resolve. Can be:
'a.b.c' or 'a[0].b.c'['a', 'b', 'c'] or ['a', 0, 'b', 'c']*, optional): The value returned if the resolved value is undefinedReturns:
*): 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 existKey Behavior:
'a.b.c') and array (['a', 'b', 'c']) path notation'arr[0].prop') and array (['arr', 0, 'prop']) formatsthis)defaultValue if the path doesn't exist or resolves to undefinednull or undefined objects without throwing errorsUsage Examples:
var result = require('lodash.result');
// Complex nested object with functions
var userData = {
profile: {
name: 'John Doe',
getFullName: function() {
return this.name + ' (verified)';
},
settings: {
theme: 'dark',
notifications: {
email: true,
push: false
}
}
},
posts: [
{ title: 'First Post', views: 100 },
{ title: 'Second Post', views: 250 }
]
};
// String path notation
result(userData, 'profile.name');
// => 'John Doe'
// Function resolution with context
result(userData, 'profile.getFullName');
// => 'John Doe (verified)'
// Deep nested property access
result(userData, 'profile.settings.notifications.email');
// => true
// Array index access
result(userData, 'posts[0].title');
// => 'First Post'
// Array notation for complex paths
result(userData, ['posts', 1, 'views']);
// => 250
// Default value for missing paths
result(userData, 'profile.age', 'Unknown');
// => 'Unknown'
result(userData, 'nonexistent.deeply.nested.path', 'Not found');
// => 'Not found'
// Safe access on null/undefined objects
result(null, 'any.path', 'Safe default');
// => 'Safe default'
result(undefined, 'any.path', 'Safe default');
// => 'Safe default'Common Use Cases:
TypeError on undefined intermediate values