Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

Lodash _.prototype.chain() Method

  • Last Updated : 18 Sep, 2020

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

The _.prototype.chain() method of Sequence in lodash is used to create an instance of lodash wrapper accompanied by explicit method chain sequences enabled.

Hey geek! The constant emerging technologies in the world of web development always keeps the excitement for this subject through the roof. But before you tackle the big projects, we suggest you start by learning the basics. Kickstart your web development journey by learning JS concepts with our JavaScript Course. Now at it's lowest price ever!

Syntax:

_.prototype.chain()

Parameters: This method doesn’t accept any parameter.



Return Value: This method returns the new lodash wrapper instance.

Example 1:

Javascript




// Requiring lodash library
const _ = require('lodash');
  
// Initializing authors variable
var 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:

Javascript




// Requiring lodash library
const _ = require('lodash');
  
// Initializing authors variable
var 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:

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




My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!