Lodash _.zipObject() method
What you’ll learn
- How
_.zipObject(props, values)pairs each key with the value at the same index. - How padding differs when keys outrun values (and vice versa).
- How this relates to
_.zip()and_.fromPairs(). - Try each example in the editor (
?tryit=1,2,3) with Lodash from a CDN.
Prerequisites
Skim _.zip() if bundling columns into tuples first clicks better than jumping straight to objects.
- You understand plain objects map string keys (after coercion) to arbitrary values.
- You can open Try-it labs or run snippets locally.
Overview
_.zipObject is the ergonomic bridge when APIs hand you one array of field names and another of cells—think CSV headers plus data row, or GraphQL aliases paired with scalars.
Aligned indices
Index i binds props[i] to values[i] without manual loops.
Fresh object
Inputs remain untouched; reuse arrays across retries safely.
Zip sibling
_.fromPairs(_.zip(keys, vals)) matches zipObject(keys, vals) for typical arrays.
Syntax
_.zipObject(props, values) - props: array of property identifiers (coerced to strings when assigned).
- values: parallel array of values; extras beyond
props.lengthare ignored. - Returns: new plain object pairing each prop with its aligned value (or
undefinedwhen values run short).
Headers and cells
Turn matching-length arrays into one record—ideal after validating parallel CSV columns.
import zipObject from "lodash/zipObject";
zipObject(["name", "score"], ["ada", 100]);
// → { name: "ada", score: 100 } More keys than values
Trailing keys map to undefined. Remember JSON.stringify drops keys whose values are undefined, even though the object still owns those properties.
import zipObject from "lodash/zipObject";
zipObject(["x", "y", "z"], [10]);
// → { x: 10, y: undefined, z: undefined } Duplicate keys and fromPairs(zip(...))
Repeated props behave like repeated assignments—last value wins. For the same shapes, zipObject(keys, vals) matches fromPairs(zip(keys, vals)).
import zipObject from "lodash/zipObject";
import fromPairs from "lodash/fromPairs";
import zip from "lodash/zip";
zipObject(["role", "role"], ["viewer", "admin"]);
// → { role: "admin" }
const keys = ["a", "b"];
const vals = [1, 2];
zipObject(keys, vals);
// same pairing as:
fromPairs(zip(keys, vals));
// → { a: 1, b: 2 } 📋 _.zipObject vs zip, fromPairs, zipObjectDeep
| API | Shape | Use case |
|---|---|---|
_.zipObject(props, values) | Two parallel arrays | Fast object build when headers and row cells already align |
_.zip(...arrays) | Columns → tuples | Keep intermediate arrays before reducing or mapping |
_.fromPairs(pairs) | Array of [key, value] | Consume iterators shaped like Object.entries |
_.zipObjectDeep(props, values) | Nested paths | Materialize trees from parallel path strings |
Pitfalls to avoid
undefined disappears
JSON.stringify omits undefined-valued keys—use placeholders or strip keys explicitly when APIs forbid holes.
Duplicate collisions
Later duplicates overwrite silently—dedupe first if every slot must survive.
Flat assignment only
Nested structures need manual assignment or zipObjectDeep; plain zipObject does not parse dotted paths.
❓ FAQ
Summary
- Purpose:
_.zipObject(props, values)builds an object by zipping keys with aligned values. - Contrast: switch to fromPairs when data already arrives as pair rows.
- Next: Lodash _.zipObjectDeep(), _.zip() (previous), or the array methods hub.
_.zipObject is the object-shaped sibling of _.zip()—parallel columns become properties instead of inner arrays.
6 people found this page helpful
