zipObject()
zipObject<
K,V>(keys,values):Record<K,V>
Creates an object composed from arrays of keys and values.
💎 Why is this a Hidden Gem?
A concise way to create typed objects from parallel key-value arrays.
DEPRECATED
Use Object.fromEntries() with map() or zip() directly instead.
Type Parameters
K: K extends PropertyKey
The type of keys (must be valid property key).
V: V
The type of values.
Parameters
keys: readonly K[]
The property keys.
values: readonly V[]
The property values.
Returns: Record<K, V>
The new object.
See Also
Since
2.0.0
Also known as
fromEntries (Effect) · fromPairs (Remeda) · objectify (Radashi) · zipObj (Ramda) · zipObject (Lodash, es-toolkit) · ❌ (Modern Dash, Antfu)
Example
// ❌ Deprecated approach
zipObject(['a', 'b'], [1, 2]);
// => { a: 1, b: 2 }
// ✅ Recommended approach
Object.fromEntries(['a', 'b'].map((k, i) => [k, [1, 2][i]]));
// => { a: 1, b: 2 }
// Or using zip from Arkhe
import { zip } from '@pithos/arkhe/array/zip';
Object.fromEntries(zip(['a', 'b'], [1, 2]));
// => { a: 1, b: 2 }
How it works?
Creates an object from arrays of keys and values.
Processing Flow
Common Inputs
| Keys | Values | Result |
|---|---|---|
['a', 'b'] | [1, 2] | {a: 1, b: 2} |
['x', 'y', 'z'] | [10, 20] | {x: 10, y: 20, z: undefined} |
Deprecated: Use
Object.fromEntries()withmap()orzip().
Use Cases
Create object from arrays 📌
Create an object from arrays of keys and values.
const keys = ["a", "b", "c"];
const values = [1, 2, 3];
Object.fromEntries(keys.map((k, i) => [k, values[i]]));
// => { a: 1, b: 2, c: 3 }
Map fields to values
Build an object from parallel arrays.
const fields = ["name", "email", "age"];
const data = ["Alice", "alice@example.com", 30];
Object.fromEntries(fields.map((f, i) => [f, data[i]]));
// => { name: "Alice", email: "alice@example.com", age: 30 }
Combine headers with row
Create object from CSV header and data row.
const headers = ["id", "title", "price"];
const row = [1, "Product", 99.99];
Object.fromEntries(headers.map((h, i) => [h, row[i]]));
Deprecated: Use