Wednesday, July 3, 2024
HomeLanguagesJavascriptUnderscore.js _.chain() Function

Underscore.js _.chain() Function

Underscore.js is a JavaScript library that provides a lot of useful functions that help in the programming in a big way like the map, filter, invoke, etc even without using any built-in objects.

The _.chain() function is an inbuilt function in 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: It accepts a single parameter which is specified below:

  • obj: It is the stated object.

Return Value: This method returns a wrapped object.

Example 1:




<!DOCTYPE html>
<html>
  
<head>
    <script src=
    </script>
</head>
  
<body>
    <script>
        console.log(_.chain([99, 3, 4, 6]).push(100));
    </script>
</body>
  
</html>


Output:

[99, 3, 4, 6, 100]

Example 2:




<!DOCTYPE html>
<html>
  
<head>
    <script src=
    </script>
</head>
  
<body>
    <script>
        var author_article = [
            { author: 'Nidhi1352', articles: 792 },
            { author: 'Nisha95', articles: 590 },
            { author: 'Rohit01', articles: 450 }
        ];
          
        var 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

Whether you’re preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, neveropen Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we’ve already empowered, and we’re here to do the same for you. Don’t miss out – check it out now!

Thapelo Manthata
I’m a desktop support specialist transitioning into a SharePoint developer role by day and Software Engineering student by night. My superpowers include customer service, coding, the Microsoft office 365 suite including SharePoint and power platform.
RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments