Table of contents
1.
Introduction
2.
Why This Function is Used
3.
Syntax, Parameter and Return Value
3.1.
Syntax:
3.2.
Parameters:
3.3.
Return Value: 
4.
Examples 
4.1.
Dynamic Method Invocation:
4.2.
JavaScript
4.3.
Using with Collections:
4.4.
JavaScript
4.5.
Accessing Nested Object Methods:
4.6.
JavaScript
4.7.
Conditional Method Execution:
4.8.
JavaScript
5.
Frequently Asked Questions
5.1.
Can _.method() handle methods with multiple arguments?
5.2.
How does _.method() differ from directly invoking object methods?
5.3.
Is _.method() suitable for all objects and methods?
6.
Conclusion
Last Updated: Mar 27, 2024
Easy

Lodash _.method() Method

Author Gaurav Gandhi
0 upvote

Introduction

In programming, particularly with object-oriented JavaScript, invoking methods on objects dynamically based on certain conditions or inputs is a common practice. Lodash's _.method() function provides a streamlined way to create a function that calls a method on a given object. 

Lodash _.method() Method

This method is especially useful in scenarios where the method to be called is determined at runtime, or when integrating method calls into functional programming patterns.

Why This Function is Used

The _.method() function is used to create a function that invokes the method at a given path of an object. This allows for more flexible and dynamic method invocation, as the method and its arguments can be determined at the time of execution, rather than being hardcoded.

Syntax, Parameter and Return Value

Syntax:

 _.method(path, [args])

Parameters:

  • path (Array|string): The path of the method to invoke.
     
  • [args] (...*): The arguments to invoke the method with.

Return Value: 

(Function) - Returns the new invoker function.

Examples 

Dynamic Method Invocation:

  • JavaScript

JavaScript

var _ = require('lodash');

var object = {

 data: [1, 2, 3],

 getData: function(index) { return this.data[index]; }

};

var getDataMethod = _.method('getData', 1);

console.log(getDataMethod(object));
You can also try this code with Online Javascript Compiler
Run Code

Output: 

2


Demonstrates creating a function to invoke a method of an object with a specific argument.

Using with Collections:

  • JavaScript

JavaScript

var users = [

 { 'user': 'barney', 'age': 36, 'greet': function() { return 'Hi ' + this.user; } },

 { 'user': 'fred', 'age': 40, 'greet': function() { return 'Hello ' + this.user; } }

];

var greetMethod = _.method('greet');

var greetings = _.map(users, greetMethod);

console.log(greetings);
You can also try this code with Online Javascript Compiler
Run Code

Output:

 ['Hi barney', 'Hello fred']


Shows creating a function to invoke a method on each object in a collection.

Accessing Nested Object Methods:

  • JavaScript

JavaScript

var store = {

 checkout: {

   processPayment: function(amount) { return 'Processed ' + amount; }

 }

};

var processPayment = _.method(['checkout', 'processPayment'], 100);

console.log(processPayment(store));
You can also try this code with Online Javascript Compiler
Run Code

Output:

 'Processed 100'


An example of using _.method() to access and invoke a method of a nested object.

Conditional Method Execution:

  • JavaScript

JavaScript

var rectangle = {

 width: 10,

 height: 20,

 area: function() { return this.width * this.height; },

 perimeter: function() { return 2 * (this.width + this.height); }

};

var getArea = _.method('area');

var getPerimeter = _.method('perimeter');

console.log(getArea(rectangle));

console.log(getPerimeter(rectangle));
You can also try this code with Online Javascript Compiler
Run Code

Output: 

200
60


Demonstrates using _.method() for conditional execution of object methods based on runtime requirements.

Frequently Asked Questions

Can _.method() handle methods with multiple arguments?

Yes, _.method() can handle methods with multiple arguments by specifying them in the [args] parameter.

How does _.method() differ from directly invoking object methods?

_.method() provides a way to create a reusable function that can dynamically invoke object methods, which is particularly useful in higher-order functions or when the object or method is not known until runtime.

Is _.method() suitable for all objects and methods?

_.method() is suitable for most objects and methods, but it requires that the method path is correctly specified and the object structure adheres to the expected format.

Conclusion

Lodash's _.method() function is a handy tool for creating functions that invoke specified methods on objects, adding flexibility and dynamism to method invocation, especially useful in functional programming patterns and when dealing with dynamic or variable object structures.

You can refer to our guided paths on the Coding Ninjas. You can check our course to learn more about DSADBMSCompetitive ProgrammingPythonJavaJavaScript, etc. 

Also, check out some of the Guided Paths on topics such as Data Structure and AlgorithmsCompetitive ProgrammingOperating SystemsComputer Networks, DBMSSystem Design, etc., as well as some Contests, Test Series, and Interview Experiences curated by top Industry Experts.

Live masterclass