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