What is lodash.get?
The lodash.get package is a method from the Lodash library that allows for safe access to nested object properties. It helps in avoiding errors when querying for deeply nested properties in an object that may or may not exist.
What are lodash.get's main functionalities?
Accessing Nested Object Properties
Safely access nested object properties using a string path.
const object = { 'a': [{ 'b': { 'c': 3 } }] };
const value = _.get(object, 'a[0].b.c');
console.log(value); // => 3
Default Values
Provide a default value if the resolved value is undefined.
const object = { 'a': [{ 'b': { 'c': 3 } }] };
const value = _.get(object, 'a[0].b.d', 'default');
console.log(value); // => 'default'
Accessing Properties with Dynamic Paths
Use an array to specify the path for properties, allowing for dynamic path construction.
const object = { 'a': [{ 'b': { 'c': 3 } }] };
const path = ['a', '0', 'b', 'c'];
const value = _.get(object, path);
console.log(value); // => 3
Other packages similar to lodash.get
object-path
object-path is similar to lodash.get in that it provides access to nested object properties. It offers a similar API but does not have the same level of utility functions that lodash provides.
dlv
dlv is a tiny utility similar to lodash.get for safely accessing deep properties of objects. It is smaller in size compared to lodash.get, which might be beneficial for performance-sensitive applications.
dot-prop
dot-prop allows for similar functionality to lodash.get with the ability to get, set, or delete properties from a nested object using a dot path. It differs in API and does not provide the same default value feature as lodash.get.
lodash.get v4.4.2
The lodash method _.get
exported as a Node.js module.
Installation
Using npm:
$ {sudo -H} npm i -g npm
$ npm i --save lodash.get
In Node.js:
var get = require('lodash.get');
See the documentation or package source for more details.