Lodash _.thru() Method
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
Contact Us