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.
Invoking a Method on Array Elements:
4.2.
JavaScript
4.3.
Applying a Method to Objects in an Array:
4.4.
JavaScript
4.5.
Using a Custom Function:
4.6.
JavaScript
4.7.
Invoking with Additional Arguments:
4.8.
JavaScript
5.
Frequently Asked Questions 
5.1.
How does _.invokeMap() handle elements that don't have the specified method?
5.2.
Can _.invokeMap() be used with a method that modifies the original elements?
5.3.
Is _.invokeMap() efficient for large collections?
6.
Conclusion
Last Updated: Mar 27, 2024
Easy

Lodash _.invokeMap() Method

Author Riya Singh
0 upvote

Introduction

Manipulating collections by invoking a method on each element can streamline complex operations in JavaScript. This is where Lodash's _.invokeMap() comes into play. It applies a specified method to each element in a collection, which can be especially useful when dealing with arrays of objects or nested structures. 

Lodash _.invokeMap() Method

This article will explore the _.invokeMap() method, highlighting its functionality, syntax, and practical applications through examples and FAQs.

Why This Function is Used

The _.invokeMap() function is used for invoking a specified method on each element in a collection. It's particularly useful in scenarios where a uniform operation needs to be performed on all elements, such as transforming data, applying a function, or extracting information. This method simplifies the process of applying consistent transformations across a collection, enhancing code efficiency and readability.

Syntax, Parameter and Return Value

Syntax:

 _.invokeMap(collection, path, [args])

Parameters:

  • collection (Array|Object): The collection to iterate over.
     
  • path (Array|string): The path of the method to invoke or the function to invoke.
     
  • [args] (...*): The arguments to invoke the method with.

Return Value: 

(Array) - Returns the result of invoking the method on each element.

Examples 

Invoking a Method on Array Elements:

  • JavaScript

JavaScript

var _ = require('lodash');

var strings = ['123', '456', '789'];

var parsedIntegers = _.invokeMap(strings, parseInt);

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

 Output: 

[123, 456, 789]


Demonstrates invoking parseInt on each string in the array.

Applying a Method to Objects in an Array:

  • JavaScript

JavaScript

var objects = [{ 'value': '10' }, { 'value': '20' }, { 'value': '30' }];

var values = _.invokeMap(objects, 'value.toString');

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

Output: 

['10', '20', '30']


Shows invoking the toString method on a property of each object.

Using a Custom Function:

  • JavaScript

JavaScript

var numbers = [1, 2, 3];

var doubled = _.invokeMap(numbers, num => num * 2);

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

Output:

 [2, 4, 6]


An example of invoking a custom function to double each number.

Invoking with Additional Arguments:

  • JavaScript

JavaScript

var collections = [['a', 'b'], ['c', 'd']];

var joinedStrings = _.invokeMap(collections, 'join', '-');

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

 Output: 

['a-b', 'c-d']


Demonstrates invoking the join method with an additional argument.

Frequently Asked Questions 

How does _.invokeMap() handle elements that don't have the specified method?

If an element doesn’t have the specified method, _.invokeMap() will return undefined for that element.

Can _.invokeMap() be used with a method that modifies the original elements?

Yes, if the invoked method modifies the elements, those changes will reflect in the original collection.

Is _.invokeMap() efficient for large collections?

_.invokeMap() is generally efficient, but its performance depends on the complexity of the method being invoked and the size of the collection.

Conclusion

Lodash's _.invokeMap() method is a powerful and flexible tool for uniformly applying a method or function to each element in a collection. It simplifies complex data transformations and method invocations, making code more concise and readable.

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