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