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

Lodash _.method() Method

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

Lodash _.method() method creates a function that invokes the method at the path of a given object. Any additional arguments are provided to the invoked method.

Syntax: 

_.method(path, args);

Parameters:

  • path: This is the path to invoke.
  • args: These are the arguments to invoke the method with.

Return Value:

This method returns the new invoker function.

Example 1: In this example, we are using the lodash _.method() method.

Javascript




// Requiring the lodash library 
const _ = require("lodash");
 
// Use of _.method() method
let gfg = [
    { 'a': { 'b': _.constant("geeks") } },
    { 'a': { 'b': _.constant("for") } },
    { 'a': { 'b': _.constant("geeks") } }
];
 
let ans = _.map(gfg, _.method('a.b'));
 
// Printing the output 
console.log(ans);


Output:

["geeks", "for", "geeks"]

Example 2: In this example, we are using the lodash _.method() method.

Javascript




// Requiring the lodash library 
const _ = require("lodash");
 
// Use of _.method() method
let gfg = [
    { 'b': _.constant(1) },
    { 'b': _.constant(5) },
    { 'b': _.constant(8) }
];
 
let ans = _.map(gfg, _.method(['b']));
 
// Printing the output 
console.log(ans);


Output :

[1, 5, 8]

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