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.
Applying Multiple Validations:
4.2.
JavaScript
4.3.
Aggregating Multiple Transformations:
4.4.
JavaScript
4.5.
Combining Data Analysis Functions:
4.6.
JavaScript
4.7.
Conditional Function Execution:
4.8.
JavaScript
5.
Frequently Asked Questions
5.1.
How does _.over() differ from _.flow() or _.flowRight()?
5.2.
Can _.over() handle asynchronous functions?
5.3.
Is _.over() efficient for large arrays or complex functions?
6.
Conclusion
Last Updated: Mar 27, 2024
Easy

Lodash _.over() Method

Author Rinki Deka
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

In the realm of functional programming and data manipulation, orchestrating multiple functions to work together on the same set of inputs is a common requirement. Lodash's _.over() method elegantly addresses this need. 

Lodash _.over() Method

It creates a function that invokes each provided function with the arguments it receives and returns their results. This method is particularly useful in scenarios where you need to apply multiple operations or checks to the same data and aggregate their results.

Why This Function is Used

The _.over() function is used to combine multiple functions into a single function that computes all of their results on the same input. It's essential in scenarios where multiple perspectives or operations on the same data are required, such as in validation, transformation, or analysis tasks, enabling a more modular and composable approach to function application.

Syntax, Parameter and Return Value

Syntax: 

_.over([iteratees=[_.identity]])

Parameters:

[iteratees=[_.identity]] (...(Function|Function[])): The functions to invoke.

Return Value: 

(Function) - Returns the new function.

Examples 

Applying Multiple Validations:

  • JavaScript

JavaScript

var _ = require('lodash');

var isEven = n => n % 2 === 0;

var isPositive = n => n > 0;

var checkNumber = _.over([isEven, isPositive]);

console.log(checkNumber(4));

console.log(checkNumber(-3));
You can also try this code with Online Javascript Compiler
Run Code

 

Output: 

[true, true]
[false, false]


Demonstrates using _.over() to apply multiple validation functions to a single input.

Aggregating Multiple Transformations:

  • JavaScript

JavaScript

var double = n => n * 2;

var square = n => n * n;

var transform = _.over([double, square]);

console.log(transform(5));
You can also try this code with Online Javascript Compiler
Run Code

Output:

 [10, 25]


Shows applying multiple transformation functions to the same input.

Combining Data Analysis Functions:

  • JavaScript

JavaScript

var average = arr => _.sum(arr) / arr.length;

var max = arr => _.max(arr);

var min = arr => _.min(arr);

var analyzeNumbers = _.over([average, max, min]);

console.log(analyzeNumbers([1, 2, 3]));
You can also try this code with Online Javascript Compiler
Run Code

Output:

 [2, 3, 1]


An example of using _.over() to perform multiple data analysis operations on an array.

Conditional Function Execution:

  • JavaScript

JavaScript

var greet = name => 'Hello, ' + name;

var farewell = name => 'Goodbye, ' + name;

var greetOrFarewell = _.over([greet, farewell]);

console.log(greetOrFarewell('Alice'));
You can also try this code with Online Javascript Compiler
Run Code

Output: 

['Hello, Alice', 'Goodbye, Alice']


Demonstrates conditional execution of different functions based on the same input.

Frequently Asked Questions

How does _.over() differ from _.flow() or _.flowRight()?

While _.flow() and _.flowRight() compose functions in a sequence (the output of one function is the input to the next), _.over() applies all functions to the same input independently and aggregates their results.

Can _.over() handle asynchronous functions?

_.over() is designed for synchronous functions. For asynchronous functions, you would need to handle promises or async/await patterns.

Is _.over() efficient for large arrays or complex functions?

The efficiency of _.over() depends on the complexity of the functions involved and the size of the input data. For computationally intensive tasks or large datasets, performance considerations may require optimized or parallelized approaches.

Conclusion

Lodash's _.over() method is a powerful tool for combining multiple functions to be applied to the same set of inputs, providing a straightforward way to aggregate results from diverse operations. It's particularly useful for scenarios requiring parallel function execution and result aggregation in data processing and validation tasks.

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