Table of contents
1.
Introduction
2.
Why This Function is Used
3.
Syntax, Parameter and Return Value
3.1.
Syntax:
3.2.
Parameters:
3.3.
Return Value: 
4.
Examples 
4.1.
Converting Property Name to Callback:
4.2.
JavaScript
4.3.
Object Predicate for Filtering:
4.4.
JavaScript
4.5.
Using Functions as Iteratees:
4.6.
JavaScript
4.7.
Path-Based Property Access:
4.8.
JavaScript
5.
Frequently Asked Questions
5.1.
Is _.iteratee() necessary for simple operations?
5.2.
How does _.iteratee() enhance code flexibility?
5.3.
Can _.iteratee() handle deep property paths?
6.
Conclusion
Last Updated: Mar 27, 2024
Easy

Lodash _.iteratee() Method

Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

In modern JavaScript and functional programming, transforming arguments into functions that can be used in other higher-order functions is a common practice. Lodash's _.iteratee() method plays a pivotal role in this, as it converts its argument into a callback function. 

Lodash _.iteratee() Method

This method is particularly useful in cases where you need to create flexible and reusable predicates or iteratees for operations like filtering, mapping, or sorting.

Why This Function is Used

The _.iteratee() function is used to convert various types of arguments (like objects, functions, or property names) into callback functions that return predictable results based on those arguments. It's essential in making code more flexible and concise, especially when dealing with higher-order functions that expect callbacks, such as _.filter(), _.map(), and _.sortBy().

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

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

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

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

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 DSADBMSCompetitive ProgrammingPythonJavaJavaScript, etc. 

Also, check out some of the Guided Paths on topics such as Data Structure and AlgorithmsCompetitive ProgrammingOperating SystemsComputer Networks, DBMSSystem Design, etc., as well as some Contests, Test Series, and Interview Experiences curated by top Industry Experts.

Live masterclass