Open In App

Lodash _.accessor() Method

Improve
Improve
Like Article
Like
Save
Share
Report

The Lodash _.accessor() method returns a function that will attempt to look up a given field in the given object.

Syntax:

_.accessor( field_name );

Parameters: This method takes single parameter as mentioned above and described below:

  • field_name: This method takes the field to be searched in the given object.

Return Value: This method returns a function that will attempt to look up a given field in the given object.

Note: This will not work in normal JavaScript because it requires the lodash.js contrib library to be installed. Lodash.js contrib library can be installed using npm install lodash-contrib.

Example 1:

Javascript




// Defining underscore lodash variable 
var _ = require('lodash-contrib'); 
  
var ob = { gfg : "GeeksforGeeks"
var gfgg = _.accessor('gfg'); 
    
console.log(gfgg(ob));


Output:

GeeksforGeeks

Example 2:

Javascript




// Defining underscore lodash variable 
var _ = require('lodash-contrib'); 
  
var ob = { gfg : "GeeksforGeeks"
var gfgg = _.accessor('geeks'); 
  
console.log(gfgg(ob));


Output:

undefined

Last Updated : 30 Sep, 2020
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads