How Lodash _.map Handles JavaScript Map Objects

In the Lodash utility library, _.map handles native JavaScript ES6 Map objects fundamentally differently than standard arrays or plain objects. Rather than unpacking the internal key-value entries stored in the Map, Lodash treats the Map instance as a generic non-array object. Consequently, the iteratee does not execute over the Map entries at all, executing only over any enumerable object properties manually assigned to the instance itself.

The Collection Detection Mechanism

When _.map receives a collection, it determines iteration behavior by evaluating isArrayLike(collection). A native JavaScript Map instance defines a .size property rather than a numeric .length property, causing isArrayLike to return false.

Because it fails the array-like check, Lodash routes the Map through its internal object iteration path (baseForOwn), which inspects the object using standard property enumeration methods equivalent to Object.keys().

Internal Slots vs. Enumerable Properties

A JavaScript Map stores its entries in private internal slots accessed via methods such as .set(), .get(), and the [Symbol.iterator] protocol. These entries are not own enumerable string or symbol properties of the Map object:

Differences from Native Map Iteration

Lodash's execution model contrasts sharply with both native Map.prototype.forEach and standard array mapping:

  1. Native Map.prototype.forEach: Executes its callback for every entry with callback(value, key, map), preserving entry types and ignoring arbitrary non-entry properties.
  2. Lodash _.map on Arrays and Objects: Unpacks values and keys/indices automatically, calling iteratee(value, index|key, collection).
  3. Lodash _.map on Converted Maps: If the Map is converted to an iterable array via Array.from(myMap) or [...myMap], _.map iterates over arrays of entries. In this state, the iteratee receives (entry, index, array), where entry is a two-element array [key, value], rather than distinct value and key parameters.