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

Lodash _.tap() Method

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

Lodash _.tap() method of Sequence in lodash is used to call interceptor. Moreover, the main task of the method is to “tap into” a method chain sequence so that the intermediate results can be modified.

Syntax:

_.tap(value, interceptor);

Parameters:

  • value: It is the value to be given to the interceptor.
  • interceptor: It is the function to be called.

Return Value:

This method returns the value.

Example 1: In this example, we are printing the array in the console by operating some other function on this with the help of the lodash _.tap() method.

Javascript




// Requiring lodash library
const _ = require('lodash');
 
// Calling tap() method
let result = _([5, 6, 7]).tap(function (arr) {
 
    // Modifying input array using push
    // operation
    arr.push(8);
})
    .value();
 
// Displays output
console.log(result);


Output:

[ 5, 6, 7, 8 ]

Example 2: In this example, we are printing the array in the console by operating some other function on this with the help of the lodash _.tap() method.

Javascript




// Requiring lodash library
const _ = require('lodash');
 
// Calling tap() method
let result = _(['Geeks', 'for']).tap(function (arr) {
 
    // Modifying input array using push
    // operation
    arr.push('Geeks');
})
    .value();
 
// Displays output
console.log(result);


Output:

[ 'Geeks', 'for', 'Geeks' ]

Example 3: In this example, we are printing the array in the console by operating pop and tail method on this with the help of the lodash _.tap() method.

Javascript




// Requiring lodash library
const _ = require('lodash');
 
// Calling tap() method
let result = _(['f', 'g', 'h']).tap(function (arr) {
 
    // Modifying input array using pop
    // operation
    arr.pop();
})
    .tail()     // Using tail() method
    .value();
 
// Displays output
console.log(result);


Output:

[ 'g' ]

Reference: https://lodash.com/docs/4.17.15#tap


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