Syntax, Parameter and Return Value
Syntax:
_.iteratee([func=_.identity])
Parameters:
[func=_.identity]: The value to convert to a callback function. It can be a function, object, property name, or property path.
Return Value:
(Function) - Returns the callback function.
Examples
Converting Property Name to Callback:
JavaScript
var _ = require('lodash');
var users = [{ 'user': 'fred', 'age': 48 }, { 'user': 'barney', 'age': 36 }];
var callback = _.iteratee('user');
var names = _.map(users, callback);
console.log(names);

You can also try this code with Online Javascript Compiler
Run Code
Output:
['fred', 'barney']
Demonstrates using _.iteratee() to create a function that gets the 'user' property from each object.
Object Predicate for Filtering:
JavaScript
var tasks = [
{ 'title': 'Write code', 'completed': true },
{ 'title': 'Test app', 'completed': false }
];
var findCompleted = _.iteratee({ 'completed': true });
var completedTasks = _.filter(tasks, findCompleted);
console.log(completedTasks);

You can also try this code with Online Javascript Compiler
Run Code
Output:
[{ 'title': 'Write code', 'completed': true }]
Shows how to create a predicate function for filtering based on object properties.
Using Functions as Iteratees:
JavaScript
var square = n => n * n;
var squareIteratee = _.iteratee(square);
var squaredNumbers = _.map([1, 2, 3], squareIteratee);
console.log(squaredNumbers);

You can also try this code with Online Javascript Compiler
Run Code
Output:
[1, 4, 9]
An example of using a custom function as an iteratee.
Path-Based Property Access:
JavaScript
var objects = [{ 'a': { 'b': { 'c': 3 } } }, { 'a': { 'b': { 'c': 4 } } }];
var pathIteratee = _.iteratee('a.b.c');
var values = _.map(objects, pathIteratee);
console.log(values);

You can also try this code with Online Javascript Compiler
Run Code
Output:
[3, 4]
Demonstrates creating an iteratee function to access nested object properties.
Frequently Asked Questions
Is _.iteratee() necessary for simple operations?
For simple operations, directly passing a function or property name might suffice. _.iteratee() becomes more useful when you need to handle various types of iteratees or create more complex predicates.
How does _.iteratee() enhance code flexibility?
_.iteratee() allows for different types of arguments to be uniformly treated as callback functions, making it easier to write generic and reusable code components.
Can _.iteratee() handle deep property paths?
Yes, _.iteratee() can handle deep property paths, allowing access to nested object properties as demonstrated in the examples.
Conclusion
Lodash's _.iteratee() method is a versatile tool for converting various types of arguments into consistent callback functions. It's particularly beneficial in functional programming patterns, enhancing code flexibility and readability in operations involving collections and data transformations.
You can refer to our guided paths on the Coding Ninjas. You can check our course to learn more about DSA, DBMS, Competitive Programming, Python, Java, JavaScript, etc.
Also, check out some of the Guided Paths on topics such as Data Structure and Algorithms, Competitive Programming, Operating Systems, Computer Networks, DBMS, System Design, etc., as well as some Contests, Test Series, and Interview Experiences curated by top Industry Experts.