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> Ā Ā Ā Ā <scriptsrc= Ā Ā Ā Ā </script> </head> Ā Ā<body> Ā Ā Ā Ā <scripttype="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> Ā Ā Ā Ā <scriptsrc= Ā Ā Ā Ā </script> </head> Ā Ā<body> Ā Ā Ā Ā <scripttype="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> Ā Ā Ā Ā <scriptsrc= Ā Ā Ā Ā </script> </head> Ā Ā<body> Ā Ā Ā Ā <scripttype="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> Ā Ā Ā Ā <scriptsrc= Ā Ā Ā Ā </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]}
 
				 
					


