liveBook · Manning

_.merge

As explained in Data-Oriented Programming: Reduce software complexity

_.merge is a Lodash function that recursively combines properties from source objects into a destination object.

When one response needs data from several maps, _.merge combines their fields into a single flat map. That convenience comes with a catch: fields sharing a name create a merge conflict that must be handled deliberately. section 11.5

Choose between nesting and merging according to the shape and conflict profile you need:

  • Nest external data under a field such as extendedInfo when avoiding conflicts matters more than keeping the result flat. section 11.5
  • Merge database and API fields when a flat result is useful and overlapping field names are absent or already accounted for. section 11.5

Combining book data

Given database data containing isbn and available, and Open Library data containing title, subtitle, number_of_pages, full_title, publish_date, and publishers, calling _.merge(dataFromDb, dataFromOpenLib) produces one map containing fields from both sources. section 11.5

Table 11.1 Two ways to combine hash maps (view table figure)

 

Advantages

Disadvantages

Nesting

No need to handle conflicts.

Result is not flat.

Merging

Result is flat.

Need to handle conflicts.

Compare nesting and merging by result shape, conflict handling, and practical trade-offs. section 11.5

How Data-Oriented Programming: Reduce software complexity covers it

How deep it goes
Explains field combination, flat output, and name collisions, but stops short of detailing conflict-resolution behavior.
Examples
Demonstrates merging database book fields with Open Library Books API fields into one search response map.
What it assumes
Assumes familiarity with maps and generic data structures while reinforcing nesting and merging from concrete examples.
What you can do after
Apply via Lodash to combine maps and assess whether overlapping fields require a different design.
How it connects
Connects hash maps, nesting, merge conflicts, generic data structures, and JSON schema.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Data-Oriented Programming: Reduce software complexity. The section links point to the parts of the book it draws on.