Open In App

Lodash _.prototype.chain() Method

Last Updated : 03 Nov, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

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




// Requiring lodash library
const _ = require('lodash');
 
// Initializing authors variable
let authors = [
    { 'author': 'Nidhi', 'articles': 750 },
    { 'author': 'Nisha', 'articles': 500 }
];
 
// Calling chain() method and creating
// an explicit chaining sequence
let result = _(authors).chain()
    .tail()
    .value();
 
// Displays output                      
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




// Requiring lodash library
const _ = require('lodash');
 
// Initializing authors variable
let authors = [
    { 'author': 'Nidhi', 'articles': 750 },
    { 'author': 'Nisha', 'articles': 500 }
];
 
// Calling chain() method and creating
// an explicit chaining sequence
let result = _(authors).chain()
    .head()
    .pick('articles')
    .value();
 
// Displays output                      
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




// Requiring lodash library
const _ = require('lodash');
 
// Calling chain() method and creating
// an explicit chaining sequence
let obj = _("GeeksforGeeks").chain().value();
 
// Displays output                      
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
Previous
Next
Share your thoughts in the comments

Similar Reads