Lodash _.map() method
What you’ll learn
- How
_.map(collection, iteratee)projects each element into a new array. - Using shorthand iteratees (
"prop", paths) versus callbacks. - Why object collections still yield arrays—and what to use instead for keyed outputs.
- Try each example in the editor (
?tryit=1,2,3) with Lodash from a CDN.
Prerequisites
Comfort with native Array.prototype.map helps—Lodash extends the same mental model to plain-object collections and iteratee sugar.
- You understand arrays vs plain objects as key/value maps.
- You can open Try-it labs or run snippets locally.
Overview
_.map is the backbone of declarative data pipelines—normalize rows, extract columns, or reshape primitives before filtering or reducing downstream.
Parallel outputs
Every visit yields one mapped slot—length mirrors visitation order.
Iteratee sugar
Property strings and matchers skip boilerplate lambdas.
Chain friendly
Pairs cleanly with Lodash sequences and functional pipelines.
Syntax
_.map(collection, iteratee) - collection: array, array-like, or plain object Lodash can iterate.
- iteratee: invoked as
(value, index|key, collection); result becomes the mapped slot. - Returns: new array (never reuses the collection container).
Transform numbers with a callback
Classic projection—double each entry without imperative loops.
import map from "lodash/map";
map([1, 2, 3], (n) => n * 2);
// → [2, 4, 6] Shorthand: pluck label
Passing a string iteratee pulls that property from each object.
import map from "lodash/map";
map(
[
{ id: 1, label: "draft" },
{ id: 2, label: "published" }
],
"label"
);
// → ["draft", "published"] Walk a plain object
Iteratee receives (value, key)—the outcome is still an array aligned with visitation.
import map from "lodash/map";
map({ x: 2, y: 3 }, (value, key) => `${key}:${value}`);
// → ["x:2", "y:3"] 📋 _.map vs mapValues, flatMap, forEach
| API | Output shape | Best when |
|---|---|---|
_.map(collection, iteratee) | Array | List projections or object scans needing arrays |
_.mapValues(object, iteratee) | Object | Preserve keys while transforming values |
_.flatMap(collection, iteratee) | Array (flattened) | Iteratees return nested arrays to merge one level |
_.forEach(collection, iteratee) | undefined / chaining side effects | Fire-and-forget side effects without collecting results |
Pitfalls to avoid
Expecting keyed output
Switch to mapValues or zipObject when downstream wants the original keys retained.
In-place edits inside iteratees
map encourages purity—mutating shared references surprises consumers.
Extra passes
Chained maps allocate intermediates—fuse logic or profile before micro-tuning native loops.
❓ FAQ
Summary
- Purpose:
_.map(collection, iteratee)transforms each visited element into a new array. - Contrast: pair
flatMapwhen outputs nest; prefermapValueswhen keys must survive. - Next: Lodash _.orderBy(), Lodash _.keyBy() (previous), or collection hub.
Mapping over a plain object still yields an array of results—one slot per enumerable entry. When you need the same keys back with transformed values, reach for _.mapValues (or build your own reducer).
6 people found this page helpful
