Underscore.js _.chain() Function
Underscore.js _.chain() function is an inbuilt function in the Underscore.js library of JavaScript which is used to find a wrapped object. Moreover, invoking the methods on this object will continue to return the wrapped objects until the value is invoked.
Syntax:
_.chain(obj);
Parameters:
- obj: It is the stated object.
Return Value:
This method returns a wrapped object.
Example 1: In this example, we are using the Underscore.js _.chain() function.
HTML
<!DOCTYPE html> < html lang = "en" > < head > < meta charset = "UTF-8" > < meta name = "viewport" content = "width=device-width, initial-scale=1.0" > < title >Document</ title > </ head > < body > <!DOCTYPE html> < html > < head > < script src = "https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.9.1/underscore-min.js" > </ script > </ head > < body > < script > console.log(_.chain([99, 3, 4, 6]).push(100)); </ script > </ body > </ html > </ body > </ html > |
Output:
[99, 3, 4, 6, 100]
Example 2: In this example, we are using the Underscore.js _.chain() function.
javascript
<!DOCTYPE html> <html> <head> <script src= "https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.9.1/underscore-min.js" > </script> </head> <body> <script> let author_article = [ { author: 'Nidhi1352' , articles: 792 }, { author: 'Nisha95' , articles: 590 }, { author: 'Rohit01' , articles: 450 } ]; let experienced = _.chain(author_article) .sortBy( function (author_article) { return author_article.article; }) .map( function (author_article) { return author_article.author + ' wrote ' + author_article.articles + ' articles! ' ; }) .first() .value(); console.log(experienced); </script> </body> </html> |
Output:
Nidhi1352 wrote 792 articles!
Reference: https://underscorejs.org/#chain
Contact Us