The Wayback Machine - https://web.archive.org/web/20240119133014/https://www.geeksforgeeks.org/lodash-_-invokemap-method/
Open In App
Related Articles

Lodash _.invokeMap() Method

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Report issue
Report

Lodash_.invokeMap() method invokes the method at the given path of each element in the collection and returns an array of the results of each invoked method. Additional arguments can be provided for each invoked method using the args parameter. The given path can also be a function that would be bound to each element in the collection.

Syntax:

_.invokeMap( collection, path, args );

Parameters:

  • collection: This parameter holds the collection that has to be iterated over.
  • path: This parameter holds the path of the method to invoke or the function invoked per iteration.
  • args: This parameter holds the arguments to invoke each method with.

Return Value:

This method returns the array of results.

Example 1: In this example, we are sorting the given array by the use of the lodash _.invokeMap() method.

javascript




// Requiring the lodash library
const _ = require('lodash');
 
// Original array
let obj = [[6, 2, 8], [2, 1, 0]];
 
// Using the _.invokeMap() method
let gfg1 = _.invokeMap(obj, 'sort');
 
// Printing the output
console.log(gfg1);


Output:

[ [ 2, 6, 8], [ 0, 1, 2 ] ]

Example 2: In this example, we are spliting the given array by the use of the lodash _.invokeMap() method.

javascript




// Requiring the lodash library
const _ = require('lodash');
 
// Original array
let obj = [628, 210];
 
// Using the _.invokeMap() method
let gfg1 = _.invokeMap(obj, String.prototype.split, '');
 
// Printing the output
console.log(gfg1);


Output:

[ [ '6', '2', '8'], [ '2', '1', '0' ] ]

Example 3: In this example, we are spliting the given array by the use of the lodash _.invokeMap() method.

javascript




// Requiring the lodash library
const _ = require('lodash');
 
// Original array
let obj = ['srqp', 'tuvw'];
let obj1 = [['c', 'b', 'a'], ['f', 'e', 'd']];
 
// Using the _.invokeMap() method
let gfg = _.invokeMap(obj, String.prototype.split, '');
let gfg1 = _.invokeMap(obj1, 'sort');
 
// Printing the output
console.log(gfg, gfg1);


Output:

[ [ 's', 'r', 'q', 'p'], [ 't', 'u', 'v', 'w' ] ]
[ [ 'a', 'b', 'c'], [ 'd', 'e', 'f' ] ]

Learn to code easily with our course Coding for Everyone. This course is accessible and designed for everyone, even if you're new to coding. Start today and join millions on a journey to improve your skills!

Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out - check it out now!

Commit to GfG's Three-90 Challenge! Purchase a course, complete 90% in 90 days, and save 90% cost click here to explore.
Last Updated : 03 Nov, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads
Complete Tutorials