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: 'zambiatek' }, []
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā )
Ā Ā Ā Ā Ā Ā Ā Ā );
Ā Ā Ā Ā </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, zambiatek 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

Leave a Reply

Your email address will not be published. Required fields are marked *

Check Also
Close
Back to top button