Lodash _.zipObject() method

Beginner
⏱️ 6 min read
📚 Updated: May 2026
🎯 3 Code examples
🚀 3 Try-it labs
Lodash

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

javascript
_.zipObject(props, values)
  • props: array of property identifiers (coerced to strings when assigned).
  • values: parallel array of values; extras beyond props.length are ignored.
  • Returns: new plain object pairing each prop with its aligned value (or undefined when values run short).
1

Headers and cells

Turn matching-length arrays into one record—ideal after validating parallel CSV columns.

javascript
import zipObject from "lodash/zipObject";

zipObject(["name", "score"], ["ada", 100]);
// → { name: "ada", score: 100 }
Try it Yourself
2

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.

javascript
import zipObject from "lodash/zipObject";

zipObject(["x", "y", "z"], [10]);
// → { x: 10, y: undefined, z: undefined }
Try it Yourself
3

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)).

javascript
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 }
Try it Yourself

📋 _.zipObject vs zip, fromPairs, zipObjectDeep

APIShapeUse case
_.zipObject(props, values)Two parallel arraysFast object build when headers and row cells already align
_.zip(...arrays)Columns → tuplesKeep intermediate arrays before reducing or mapping
_.fromPairs(pairs)Array of [key, value]Consume iterators shaped like Object.entries
_.zipObjectDeep(props, values)Nested pathsMaterialize trees from parallel path strings

Pitfalls to avoid

JSON

undefined disappears

JSON.stringify omits undefined-valued keys—use placeholders or strip keys explicitly when APIs forbid holes.

Keys

Duplicate collisions

Later duplicates overwrite silently—dedupe first if every slot must survive.

Deep

Flat assignment only

Nested structures need manual assignment or zipObjectDeep; plain zipObject does not parse dotted paths.

❓ FAQ

No. Lodash reads both arrays and returns a new object; the inputs stay unchanged.
Missing values become undefined for those keys. Extra values beyond the last key are ignored.
Assignments proceed in order—the last value wins for that property name.
fromPairs consumes [[key, value], ...] rows; zipObject splits keys and values across two parallel arrays.
Use import zipObject from "lodash/zipObject" or require("lodash/zipObject") for tree-shaken bundles.

Summary

Did you know?

_.zipObject is the object-shaped sibling of _.zip()—parallel columns become properties instead of inner arrays.

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

6 people found this page helpful