The _.get() method is used to get the value at path of object. If the resolved value is undefined, the defaultValue is returned in its place.
Syntax:
_.get(object, path, [defaultValue])
Parameters: This method accepts three parameters as mentioned above and described below:
- object: This parameter holds the object to query.
- path: This parameter holds the path of the property to get. The path will be array or string.
- defaultValue: This parameter holds the value returned for undefined resolved values.
Return Value: This method returns the resolved value.
Example 1:
Javascript
const _ = require( "lodash" );
var object = { 'c' : [{ 'python' : { 'java' : 3 } }] };
console.log(_.get(object, 'c[0].python.java' ));
|
Output:
3
Example 2:
Javascript
const _ = require( "lodash" );
var object = { 'c' : [{ 'python' : { 'java' : 3 } }] };
console.log(_.get(object, [ 'c' , '0' , 'python' , 'java' ]));
|
Output:
3
Example 3:
Javascript
const _ = require( "lodash" );
var object = { 'c' : [{ 'python' : { 'java' : 3 } }] };
console.log(_.get(object, 'c.python.java' , 'default' ));
|
Output:
'default'