Open In App

Lodash _.accessor() Method

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:

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:




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

Output:

GeeksforGeeks

Example 2:




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

Output:

undefined
Article Tags :