how to use the __lookupgetter__ function from lodash in javascript

The _.lookupGetter method from lodash is used to retrieve the getter function of a given property from a given object. Here is an example of how to use it:

index.tsx
const _ = require('lodash');

const person = {
  firstName: 'John',
  lastName: 'Doe',
  get fullName() {
    return `${this.firstName} ${this.lastName}`;
  },
};

const getterFunction = _.lookupGetter(person, 'fullName');

console.log(getterFunction()); // Output: John Doe
275 chars
14 lines

In the example above, we define an object person which has a getter function fullName. We use _.lookupGetter to retrieve the getter function of the fullName property and assign it to getterFunction. Finally, we invoke getterFunction to get the expected output.

Note that if the given property does not have a getter function, _.lookupGetter returns undefined.

gistlibby LogSnag