Lodash _.thru() method of Sequence in lodash is similar to _.tap() method and the only difference is that it returns the outcome of the interceptor. Moreover, this method is mainly used to âpass thruâ values in a method chain sequence by replacing intermediate results.
Syntax:
_.thru(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 result of the interceptor.
Example 1: In this example, we are getting a value as an array because of the use of the lodash _.thru() method.
Javascript
const _ = require('lodash');
let result = _(144).thru(function (value) {
return [value];
}).value();
console.log(result);
|
Output:
[ 144 ]
Example 2: In this example, we are getting a value as an array because of the use of the lodash _.thru() method.
Javascript
const _ = require('lodash');
let result = _('GfG').thru(function (value) {
return [value];
}).value();
console.log(result);
|
Output:
[ 'GfG' ]
Example 3: In this example, we are getting an array of array because of the use of the lodash _.thru() method.
Javascript
const _ = require('lodash');
let val = ['g', 'f', 'G']
let result = _(val).reverse()
.chain()
.thru(function (value) {
return [value];
})
.value();
console.log(result);
|
Output:
[ [ 'G', 'f', 'g' ] ]
Reference: https://lodash.com/docs/4.17.15#thru
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
Share your thoughts in the comments
Please Login to comment...