Lodash _.fromPairs() method

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

What you’ll learn

  • How _.fromPairs(pairs) turns [['key', value], ...] into a plain object.
  • How it pairs with _.toPairs for round-trip conversions.
  • Duplicate keys, key coercion, and how this compares to Object.fromEntries.
  • Try each example in the editor (?tryit=1, 2, 3) with Lodash from a CDN.

Prerequisites

Optional prior read _.flattenDepth(); you should be comfortable with object literals and array literals.

  • You understand that object keys are usually strings (or symbols) after assignment.
  • You can run snippets in Node or open the Try-it labs in a browser.

Overview

_.fromPairs is the array-first constructor for objects: feed it parallel [key, value] rows (often produced by _.toPairs, Object.entries, or CSV parsers) and receive one merged object. It is a small, predictable building block for transforms and serializers.

Row-oriented

Pairs are a natural shape for tables, query strings, and key-value streams.

Deterministic merges

Duplicate keys resolve to the last pair in the array, which is easy to reason about when sorted.

New object

The returned object is fresh; the pairs array is read-only for this call.

Syntax

javascript
_.fromPairs(pairs)
  • pairs: array where each element is a two-item array [key, value] (array-like outer collection is coerced).
  • Returns: a new plain object with properties assigned from each pair in order.
1

Simple key-value rows

Each inner array supplies one property name and one value on the result object.

javascript
import fromPairs from "lodash/fromPairs";

fromPairs([
  ["name", "Ada"],
  ["year", 1815]
]);
// { name: "Ada", year: 1815 }
Try it Yourself
2

Round-trip with toPairs

toPairs expands an object into rows; fromPairs folds rows back. For string keys this reconstructs a shallow-equivalent object.

javascript
import fromPairs from "lodash/fromPairs";
import toPairs from "lodash/toPairs";

const original = { a: 1, b: 2 };
fromPairs(toPairs(original));
// { a: 1, b: 2 }
Try it Yourself
3

Duplicate keys keep the last value

Treat the pairs list like ordered assignments: later rows win, which is handy after sorting by priority.

javascript
import fromPairs from "lodash/fromPairs";

fromPairs([
  ["score", 10],
  ["score", 99]
]);
// { score: 99 }
Try it Yourself

📋 _.fromPairs vs Object.fromEntries

APIInputNote
_.fromPairs(pairs)Array of [key, value] arraysLodash module; consistent with toPairs / zip workflows
Object.fromEntries(iterable)Any iterable of entriesNative ES2019; works with Map directly
Object.assign({}, ...)Spread objectsAlternative when you already have objects, not rows

Pitfalls to avoid

Shape

Pairs must look like rows

Odd-length inner arrays or missing values still assign; validate upstream if your data is messy.

Keys

Coercion surprises

Numeric-looking keys become string property names on ordinary objects unless you use Map instead.

Proto

Never feed untrusted __proto__ keys

Filter dangerous pair keys from user input before building objects, same as with JSON.parse merge patterns.

❓ FAQ

No. Lodash reads the pairs and returns a new plain object. The input array of pairs is not modified.
Later pairs overwrite earlier ones because each step assigns the same property name on the accumulating object.
fromEntries accepts any iterable of [key, value] entries (including Maps). fromPairs expects an array of pair arrays and matches classic Lodash style.
Keys are taken from the first element of each pair; JavaScript object property rules apply (string and symbol keys; non-symbols are coerced to string).
Use import fromPairs from "lodash/fromPairs" or require("lodash/fromPairs") for tree-shaking friendly bundles.

Summary

  • Purpose: _.fromPairs(pairs) builds a plain object from an array of [key, value] pairs.
  • Safety: the pairs array is not mutated; duplicate keys resolve to the last value.
  • Next: Lodash _.head(), _.flattenDepth() (earlier in this track), or the array methods hub.
Did you know?

_.toPairs(object) and _.fromPairs(array) are inverses for plain string-keyed objects when every value should round-trip through the pair representation.

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