Lodash _.get() method is used to get the value at the path of the object. If the resolved value is undefined, the default value is returned in its place.
Syntax:
_.get(object, path, [defaultValue]);
Parameters:
- object (Object) parameter holds the object to query.
- path (Array/String) parameter holds the path of the property to get.
- defaultValue (*) parameter holds the value returned for undefined resolved values or default values. and it is optional.
Return Value:
This method returns the resolved value
Example 1: In this example, we are accessing the value of an object using the path in the _.get() method
Javascript
const _ = require( "lodash" );
let object = { 'c' : [{ 'python' : { 'java' : 3 } }] };
console.log(_.get(object, 'c[0].python.java' ));
|
Output:
3
Example 2: In this example, we are accessing the value of an object using the path in the _.get() method but the keys are in a sequential manner
Javascript
const _ = require( "lodash" );
let object = { 'c' : [{ 'python' : { 'java' : 3 } }] };
console.log(_.get(object, [ 'c' , '0' , 'python' , 'java' ]));
|
Output:
3
Example 3: In this example, we are accessing the value of an object using the path in the _.get() method having a defalt value as “default”
Javascript
const _ = require( "lodash" );
let object = { 'c' : [{ 'python' : { 'java' : 3 } }] };
console.log(_.get(object, 'c.python.java' , 'default' ));
|
Output:
'default'
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!
Last Updated :
18 Oct, 2023
Like Article
Save Article