Open In App

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:



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.




<!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.




<!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.




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

Output:

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

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




<!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]}

Article Tags :