Lodash _.over: Applying Multiple Iteratees
The Lodash _.over method creates a new function that
invokes a given list of iteratees with the arguments it receives and
returns the combined results in an array. This article explains the
internal mechanics of _.over, demonstrates how it applies
multiple transformation functions to identical input arguments, and
highlights practical use cases for simplifying functional JavaScript
workflows.
How _.over Works
The _.over method accepts an array of functions
(iteratees) or passes them as individual arguments. Instead of executing
these iteratees immediately, it returns a new wrapper function.
When this wrapper function is called, it takes any number of arguments and distributes them sequentially to each iteratee. Every iteratee receives the exact same arguments in the original order. The individual return values are then collected and returned as a single array.
Conceptually, the execution behaves like this:
const over = (iteratees) => (...args) => iteratees.map(fn => fn(...args));Basic Syntax and Usage
The method signature in Lodash is:
_.over([iteratees=[_.identity]])Here is an example applying built-in Math functions to a
series of numerical arguments:
const _ = require('lodash');
// Create a function that calculates both minimum and maximum values
const minAndMax = _.over([Math.min, Math.max]);
const result = minAndMax(10, 5, 100, -4, 32);
console.log(result);
// Output: [-4, 100]In this execution:
minAndMaxis invoked with(10, 5, 100, -4, 32).Math.min(10, 5, 100, -4, 32)evaluates to-4.Math.max(10, 5, 100, -4, 32)evaluates to100.- The outputs are gathered into an array:
[-4, 100].
Applying to Complex Data Structures
Because iteratees receive all passed arguments, _.over
is effective for deriving multiple metrics or data points from a single
object or dataset without writing repetitive code.
const _ = require('lodash');
const getStats = _.over([
(items) => items.length,
(items) => _.sum(items),
(items) => _.mean(items)
]);
const numbers = [10, 20, 30, 40];
const [count, total, average] = getStats(numbers);
console.log({ count, total, average });
// Output: { count: 4, total: 100, average: 25 }Key Characteristics
- Uniform Arguments: Every function in the iteratee list receives the complete, unaltered argument list provided during the call.
- Iteratee Normalization: If a non-function value or
Lodash shorthand is supplied, Lodash converts it via
_.iterateeinto a callable function before execution. - Predictable Order: The returned array maintains the
exact positional index corresponding to the order of functions passed
into
_.over. - Contrast with Predicates: While
_.overSomeand_.overEveryevaluate boolean conditions (acting like||and&&),_.overis non-boolean and preserves all returned output values.