Lodash _.invokeMap() method
What you’ll learn
- How
_.invokeMap(collection, path, ...args)batches host-method calls across a collection. - Forwarding trailing arguments identically to every element.
- When
map, manualcall/apply, orinvokefor a single element fits better. - Try each example in the editor (
?tryit=1,2,3) with Lodash from a CDN.
Prerequisites
Skim _.includes() for membership checks and _.flatMap() when mapping needs flattening—not dispatch.
- You know methods live on prototypes (
split,slice) or as own properties on plain objects. - You can open Try-it labs or run snippets locally.
Overview
_.invokeMap removes repetitive collection.map((item) => item.doThing(arg)) boilerplate—especially when doThing already exists as a named path.
Uniform dispatch
Same method name or path executed element-by-element.
Shared arguments
Extra parameters replicate across every call automatically.
Function paths
Pass a function to run with each element bound as this.
Syntax
_.invokeMap(collection, path, [args]) - collection: array or plain object Lodash can iterate.
- path: method name string, property path, or function invoked per element.
- args (optional): forwarded to every invocation after
pathresolves. - Returns: new array of results (same length/order as visitation).
String split with a delimiter argument
Strings expose split; Lodash passes "-" to each host string.
import invokeMap from "lodash/invokeMap";
invokeMap(["a-b-c", "d-e"], "split", "-");
// → [["a", "b", "c"], ["d", "e"]] Array slice from index 1
Each inner array receives slice(1)—non-mutating trimming.
import invokeMap from "lodash/invokeMap";
invokeMap(
[
[1, 2, 3],
[4, 5]
],
"slice",
1
);
// → [[2, 3], [5]] Plain objects with their own methods
Lodash invokes double with each object as this—handy for lightweight domain rows.
import invokeMap from "lodash/invokeMap";
invokeMap(
[
{
n: 2,
double() {
return this.n * 2;
}
},
{
n: 3,
double() {
return this.n * 2;
}
}
],
"double"
);
// → [4, 6] 📋 _.invokeMap vs map, invoke
| API | Scope | Best when |
|---|---|---|
_.invokeMap(collection, path, ...args) | Every element | Batch host calls with identical arguments |
_.map(collection, iteratee) | Every element | Custom projections—logic not stored as a path |
_.invoke(object, path, ...args) | Single object | One-shot dispatch without mapping |
Pitfalls to avoid
In-place methods
sort, reverse, splice mutate—clone first when isolation matters.
Detached functions
Methods ripped off objects lose this—invokeMap intentionally resolves paths on each element.
Heavy paths
Deep property lookups per row add cost—flatten shapes before hot loops when profiling shows pain.
❓ FAQ
Summary
- Purpose:
_.invokeMap(collection, path, ...args)maps method results across a collection. - Contrast: prefer
mapwhen inline logic outgrows simple paths. - Next: Lodash _.keyBy(), Lodash _.includes() (previous), or collection hub.
Arguments after path are forwarded to every invocation—the same trailing parameters reused across the whole collection. If path is itself a function, Lodash calls it with each element bound as this.
6 people found this page helpful
