Skip to content
Related Articles
Get the best out of our app
GFG App
Open App
geeksforgeeks
Browser
Continue

Related Articles

Lodash _.thru() Method

Improve Article
Save Article
Like Article
Improve Article
Save Article
Like Article

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

The _.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: This method accepts two parameters as mentioned above and described below:

  • 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:

Javascript




// Requiring lodash library
const _ = require('lodash');
  
// Calling thru() method
let result = _(144).thru(function(value) {
   return [value];
 }).value();
  
 // Displays output
 console.log(result);


Output:

[ 144 ]

Example 2:

Javascript




// Requiring lodash library
const _ = require('lodash');
  
// Calling thru() method
let result = _('GfG').thru(function(value) {
   return [value];
 }).value();
  
 // Displays output
 console.log(result);


Output:

[ 'GfG' ]

Example 3:

Javascript




// Requiring lodash library
const _ = require('lodash');
  
// Defining value
var val = ['g', 'f', 'G']
  
// Calling thru() method along with
// reverse and chain method
let result = _(val).reverse()
                   .chain()
                   .thru(function(value) {
    return [value];
 })
 .value();
  
 // Displays output
 console.log(result);


Output:

[ [ 'G', 'f', 'g' ] ]

Here, the output is reversed as reverse() method is used above in order to reverse the order of the value stated.

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


My Personal Notes arrow_drop_up
Last Updated : 18 Sep, 2020
Like Article
Save Article
Similar Reads
Related Tutorials