Open In App

Lodash _.thru() Method

Last Updated : 03 Nov, 2023
Comments
Improve
Suggest changes
Like Article
Like
Report

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
// 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: In this example, we are getting a value as an array because of the use of the lodash _.thru() method.

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: In this example, we are getting an array of array because of the use of the lodash _.thru() method.

JavaScript
// Requiring lodash library
const _ = require('lodash');

// Defining value
let 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' ] ]

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


Similar Reads