Friday, September 27, 2024
Google search engine
HomeLanguagesJavascriptLodash _.invoke() Method

Lodash _.invoke() Method

Lodash is a JavaScript library that works on the top of underscore.js. Lodash helps in working with arrays, collection, strings, lang, function, objects, numbers etc.

The _.invoke() method is invoked the method at path of object.

Syntax:

_.invoke(object, path, args)

Parameters: This method accepts three parameters as mentioned above and described below:

  • object: It holds the object to query.
  • path: It holds the path of the method to invoke the element.
  • args: The arguments to invoke the method with.

Return Value: This method returns the result of the invoked method.

Example 1: Here, const _ = require(‘lodash’) is used to import the lodash library in the file.




// Requiring the lodash library 
const _ = require("lodash"); 
       
// Original array 
var object = { 'p': [{ 'q': { 'r': [ 3, 5, 7, 9 ] } }] };
  
// Using the _.invoke() method
let invt_elem = _.invoke(object, 'p[0].q.r.slice', 3, 7);
  
// Printing the output 
console.log(invt_elem);


Output:

[ 9 ]

Example 2:




// Requiring the lodash library 
const _ = require("lodash"); 
       
// Original array 
var object = { 'p': [{ 'q': { 'r': { 's': [ 2, 4, 6, 8, 10 ] 
  
} } }] };
  
// Using the _.invoke() method
let invt_elem = _.invoke(object, 'p[0].q.r.s.slice', 2, 5);
  
// Printing the output 
console.log(invt_elem);


Output:

[ 6, 8, 10 ]

Note: This code will not work in normal JavaScript because it requires the library lodash to be installed.

Whether you’re preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, neveropen 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!

RELATED ARTICLES

Most Popular

Recent Comments