Lodash _.prototype.chain() Method
Last Updated :
03 Nov, 2023
Lodash _.prototype.chain() method of Sequence in lodash is used to create an instance of lodash wrapper accompanied by explicit method chain sequences enabled.
Syntax:
_.prototype.chain();
Parameters:
This method doesn’t accept any parameter.
Return Value:
This method returns the new lodash wrapper instance.
Example 1: In this example, we are chaining the given array and getting the tail value by the use of the lodash _.prototype.chain() method.
Javascript
const _ = require( 'lodash' );
let authors = [
{ 'author' : 'Nidhi' , 'articles' : 750 },
{ 'author' : 'Nisha' , 'articles' : 500 }
];
let result = _(authors).chain()
.tail()
.value();
console.log(result);
|
Output:
[ { author: 'Nisha', articles: 500 } ]
Example 2: In this example, we are chaining the given array and getting the head value having article as a key by the use of the lodash _.prototype.chain() method.
Javascript
const _ = require( 'lodash' );
let authors = [
{ 'author' : 'Nidhi' , 'articles' : 750 },
{ 'author' : 'Nisha' , 'articles' : 500 }
];
let result = _(authors).chain()
.head()
.pick( 'articles' )
.value();
console.log(result);
|
Output:
{ articles: 750 }
Example 3: In this example, we are chaining the given string by the use of the lodash _.prototype.chain() method.
Javascript
const _ = require( 'lodash' );
let obj = _( "GeeksforGeeks" ).chain().value();
console.log(obj[0]);
console.log(obj[4]);
console.log(obj[7]);
console.log(obj[11]);
|
Output:
G
s
r
k
Reference: https://lodash.com/docs/4.17.15#prototype-chain
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...