Aller au contenu principal

toPairs()

toPairs<T>(object): [keyof T, T[keyof T]][]

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])
);