toPairs()
toPairs<
T>(object): [keyofT,T[keyofT]][]
toPairs(
object): []
toPairs(
object): [string,unknown][]
Creates an array of key-value pairs from an object.
π Why is this a Hidden Gem?
Unlock object iteration with a clean [key, value] tuple format.
DEPRECATED
Use Object.entries() directly instead.
Type Parametersβ
T: T extends Record<string, unknown>β
The type of the object.
Parametersβ
Overload 1:
object: Tβ
The object to convert to pairs.
Overload 2:
object: null | undefinedβ
The object to convert to pairs.
Overload 3:
object: unknownβ
The object to convert to pairs.
Returns: [keyof T, T[keyof T]][]β
An array of key-value pairs.
See Alsoβ
Sinceβ
2.0.0
Also known asβ
objectEntries (Antfu) Β· toEntries (Effect) Β· toPairs (Lodash, es-toolkit, Remeda, Ramda) Β· β (Radashi, Modern Dash)
Exampleβ
const obj = { a: 1, b: 2, c: 3 };
// β Deprecated approach
const pairs = toPairs(obj);
console.log(pairs); // [['a', 1], ['b', 2], ['c', 3]]
// β
Recommended approach
const nativePairs = Object.entries(obj);
console.log(nativePairs); // [['a', 1], ['b', 2], ['c', 3]]
How it works?β
Creates an array of key-value pairs.
Deprecated: Use Object.entries() directly (ES2017).
Native Equivalentβ
// β toPairs(obj)
// β
Object.entries(obj)
Use Casesβ
Convert to entries πβ
Convert object to array of key-value pairs.
Object.entries({ a: 1, b: 2 });
// [['a', 1], ['b', 2]]
Transform and rebuildβ
Transform object via entries.
const doubled = Object.fromEntries(
Object.entries(obj).map(([k, v]) => [k, v * 2])
);