Lodash _.prototype.chain() Method
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.
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
Please Login to comment...