Lodash _.fromPairs() method
What you’ll learn
- How
_.fromPairs(pairs)turns[['key', value], ...]into a plain object. - How it pairs with
_.toPairsfor 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
_.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.
Simple key-value rows
Each inner array supplies one property name and one value on the result object.
import fromPairs from "lodash/fromPairs";
fromPairs([
["name", "Ada"],
["year", 1815]
]);
// { name: "Ada", year: 1815 } Round-trip with toPairs
toPairs expands an object into rows; fromPairs folds rows back. For string keys this reconstructs a shallow-equivalent object.
import fromPairs from "lodash/fromPairs";
import toPairs from "lodash/toPairs";
const original = { a: 1, b: 2 };
fromPairs(toPairs(original));
// { a: 1, b: 2 } Duplicate keys keep the last value
Treat the pairs list like ordered assignments: later rows win, which is handy after sorting by priority.
import fromPairs from "lodash/fromPairs";
fromPairs([
["score", 10],
["score", 99]
]);
// { score: 99 } 📋 _.fromPairs vs Object.fromEntries
| API | Input | Note |
|---|---|---|
_.fromPairs(pairs) | Array of [key, value] arrays | Lodash module; consistent with toPairs / zip workflows |
Object.fromEntries(iterable) | Any iterable of entries | Native ES2019; works with Map directly |
Object.assign({}, ...) | Spread objects | Alternative when you already have objects, not rows |
Pitfalls to avoid
Pairs must look like rows
Odd-length inner arrays or missing values still assign; validate upstream if your data is messy.
Coercion surprises
Numeric-looking keys become string property names on ordinary objects unless you use Map instead.
Never feed untrusted __proto__ keys
Filter dangerous pair keys from user input before building objects, same as with JSON.parse merge patterns.
❓ FAQ
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.
_.toPairs(object) and _.fromPairs(array) are inverses for plain string-keyed objects when every value should round-trip through the pair representation.
6 people found this page helpful
