Monday, November 18, 2024
Google search engine
HomeLanguagesJavascriptUnderscore.js _.iteratee() Function

Underscore.js _.iteratee() 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 _.iteratee() function is an inbuilt function in Underscore which is used to generate a callback that can be applied to every element in a collection. This method supports multiple shorthand syntaxes for common callback use cases and returns the output depending on the type of the value.

Syntax:

_.iteratee( value, context )

Parameters: This method accepts two parameters as mentioned above and described below:

  • value: It is the stated value.
  • context: It is the context to be used. It is an optional parameter.

Return Value: This method returns the output depending on the type of value.

Example 1: In this example, no parameter is used with the method.

HTML




<!DOCTYPE html>
<html>
  
<head>
    <script src=
    </script>
</head>
  
<body>
    <script type="text/javascript">
  
        // Calling iteratee method with
        // no parameters
        console.log(_.iteratee());
    </script>
</body>
  
</html>


Output:

function(n){return n}

Example 2: In this example, a user-defined function is given to the method.

HTML




<!DOCTYPE html>
<html>
  
<head>
    <script src=
    </script>
</head>
  
<body>
    <script type="text/javascript">
  
        // Calling iteratee method
        // with its parameter
        console.log(
            _.iteratee(
                function (num) {
                    return num * 4;
                }
            )
        );
    </script>
</body>
  
</html>


Output:

function(num) { return num * 4; }

Example 3: In this example, a key-value pair is passed as the parameter.

HTML




<!DOCTYPE html>
<html>
  
<head>
    <script src=
    </script>
</head>
  
<body>
    <script type="text/javascript">
  
        // Calling iteratee method
        // with its parameter
        console.log(
            _.iteratee(
                { portal: 'neveropen' }, []
            )
        );
    </script>
</body>
  
</html>


Output:

function(n){return h.isMatch(n,r)}

Example 4: In this example, a string is passed as the parameter.

HTML




<!DOCTYPE html>
<html>
  
<head>
    <script src=
    </script>
</head>
  
<body>
    <script>
  
        // Calling iteratee method with
        // its parameter
        console.log(
            _.iteratee('GfG', [])
        );
    </script>
</body>
  
</html>


Output:

function(n){return null==n?void 0:n[r]}

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!

RELATED ARTICLES

Most Popular

Recent Comments