Lodash _.zipObjectDeep() method
What you’ll learn
- How
_.zipObjectDeep(props, values)turns path strings into nested objects. - When
_.zipObject()is safer (literal flat keys). - How bracket segments allocate arrays for tabular or list-shaped payloads.
- Try each example in the editor (
?tryit=1,2,3) with Lodash from a CDN.
Prerequisites
Read _.zipObject() first—the deep variant only changes how each prop string is interpreted.
- You can read simple Lodash property paths (
a.b,a[0]). - You can open Try-it labs or run snippets locally.
Overview
_.zipObjectDeep helps when configuration rows or CSV columns arrive as dotted paths—avoid dozens of set calls by zipping paths with parallel value arrays in one pass.
Parsed paths
Each key walks or creates intermediate objects and arrays automatically.
ZipObject parity
Same padding and duplicate-key semantics as shallow zipObject.
Immutable sources
Inputs remain untouched for retries with different path lists.
Syntax
_.zipObjectDeep(props, values) - props: array of property path strings (Lodash path syntax).
- values: aligned values—extras ignored; missing entries become
undefinedleaves. - Returns: new nested plain object materialized from the zipped paths.
Dotted paths merge under one parent
Separate path segments collapse into one nested record—ideal for flat env-style keys.
import zipObjectDeep from "lodash/zipObjectDeep";
zipObjectDeep(["user.name", "user.age"], ["Ada", 42]);
// → { user: { name: "Ada", age: 42 } } Bracket indices build arrays
Numeric bracket steps allocate array slots—useful when importing spreadsheet rows as parallel objects.
import zipObjectDeep from "lodash/zipObjectDeep";
zipObjectDeep(["items[0].sku", "items[1].sku"], ["A1", "B2"]);
// → { items: [{ sku: "A1" }, { sku: "B2" }] } zipObject keeps dotted keys flat
The shallow helper treats "a.b" as one literal key. The deep helper parses it into nested properties.
import zipObject from "lodash/zipObject";
import zipObjectDeep from "lodash/zipObjectDeep";
zipObject(["a.b", "a.c"], [1, 2]);
// → { "a.b": 1, "a.c": 2 }
zipObjectDeep(["a.b", "a.c"], [1, 2]);
// → { a: { b: 1, c: 2 } } 📋 _.zipObjectDeep vs zipObject, set, fromPairs
| API | Keys | Use case |
|---|---|---|
_.zipObjectDeep(props, values) | Parsed paths | Build trees from parallel path/value columns |
_.zipObject(props, values) | Literal strings | One shallow property per prop—dots stay in the key name |
_.set(object, path, value) | Single path per call | Mutate or clone-merge one branch at a time |
_.fromPairs(pairs) | First tuple slot | No automatic nesting unless keys themselves encode structure differently |
Pitfalls to avoid
Typos reshape trees
A stray dot creates an unintended subtree—validate paths when ingesting user-supplied strings.
Overlapping assignments
Later zipped pairs can replace intermediate objects built earlier—sort paths carefully when merging fragments.
Need literal dotted keys?
Stick to zipObject when keys must remain flat strings like registry.io/port.
❓ FAQ
Summary
- Purpose:
_.zipObjectDeep(props, values)pairs path strings with values to synthesize nested objects. - Contrast: prefer zipObject when keys must not be interpreted as paths.
- Next: Lodash _.zipWith(), _.zipObject() (previous), or the array methods hub.
Path strings follow the same rules as other Lodash utilities—dots traverse objects and bracket notation ([0], ["hyphen-key"]) builds arrays or awkward keys without hand-written reducers.
6 people found this page helpful
