Open In App
Related Articles

Lodash _.get() Method

Improve Article
Improve
Save Article
Save
Like Article
Like

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




// Requiring the lodash library  
const _ = require("lodash");  
  
// Given object
var object = { 'c': [{ 'python': { 'java': 3 } }] };
  
// Use of _.get method 
console.log(_.get(object, 'c[0].python.java')); 

Output:

3

Example 2:  

Javascript




// Requiring the lodash library  
const _ = require("lodash");  
  
// Given object
var object = { 'c': [{ 'python': { 'java': 3 } }] };
  
// Use of _.get method 
console.log(_.get(object, ['c', '0', 'python', 'java'])); 

Output:

3

Example 3:  

Javascript




// Requiring the lodash library  
const _ = require("lodash");  
  
// Given object
var object = { 'c': [{ 'python': { 'java': 3 } }] };
  
// Use of _.get method 
console.log(_.get(object, 'c.python.java', 'default')); 

Output:

'default'

Last Updated : 10 Sep, 2020
Like Article
Save Article
Similar Reads
Related Tutorials