0
# Lodash toPairs
1
2
The lodash toPairs function creates an array of own enumerable key-value pairs for an object. It transforms an object into an array of [key, value] pairs, making it useful for iteration, transformation, and functional programming patterns.
3
4
## Overview
5
6
The `toPairs` function is a core Lodash utility that converts objects into arrays of [key, value] pairs. It processes only own enumerable properties (excluding inherited properties from prototypes), making it safe for use with constructor functions and object inheritance. The function handles various input types including objects, strings, array-like objects, and null/undefined values gracefully.
7
8
## Package Information
9
10
- **Package Name**: lodash
11
- **Package Type**: npm
12
- **Language**: JavaScript
13
- **Installation**: `npm install lodash`
14
- **Function**: toPairs
15
- **Version**: 4.3.0
16
17
## Core Imports
18
19
```javascript
20
import { toPairs } from "lodash";
21
```
22
23
For related functionality:
24
25
```javascript
26
import { toPairs, toPairsIn } from "lodash";
27
```
28
29
For CommonJS:
30
31
```javascript
32
const { toPairs } = require("lodash");
33
```
34
35
```javascript
36
const { toPairs, toPairsIn } = require("lodash");
37
```
38
39
Full lodash import:
40
41
```javascript
42
import _ from "lodash";
43
// Use as _.toPairs() and _.toPairsIn()
44
```
45
46
```javascript
47
const _ = require("lodash");
48
// Use as _.toPairs() and _.toPairsIn()
49
```
50
51
## Basic Usage
52
53
```javascript
54
import { toPairs } from "lodash";
55
56
// Basic object conversion
57
const obj = { a: 1, b: 2, c: 3 };
58
const pairs = toPairs(obj);
59
// => [['a', 1], ['b', 2], ['c', 3]]
60
61
// String handling
62
const str = "ab";
63
const stringPairs = toPairs(str);
64
// => [['0', 'a'], ['1', 'b']]
65
66
// Object with length property
67
const arrayLike = { '0': 'first', '1': 'second', 'length': 2 };
68
const arrayLikePairs = toPairs(arrayLike);
69
// => [['0', 'first'], ['1', 'second'], ['length', 2]]
70
```
71
72
## Capabilities
73
74
### Object to Key-Value Pairs Conversion
75
76
Converts an object's own enumerable properties into an array of [key, value] pairs.
77
78
```javascript { .api }
79
/**
80
* Creates an array of own enumerable key-value pairs for `object`.
81
*
82
* @static
83
* @memberOf _
84
* @category Object
85
* @param {Object|string|null|undefined} object The object to query.
86
* @returns {Array} Returns the new array of key-value pairs.
87
*/
88
function toPairs(object);
89
```
90
91
**Behavior:**
92
- Only processes own enumerable properties (not inherited properties)
93
- Property iteration order is not guaranteed
94
- Works with objects, strings, and array-like objects
95
- Returns a new array without mutating the input
96
- Handles null/undefined input gracefully
97
98
**Usage Examples:**
99
100
```javascript
101
import { toPairs } from "lodash";
102
103
// Basic object
104
const user = { name: "Alice", age: 30 };
105
toPairs(user);
106
// => [['name', 'Alice'], ['age', 30]]
107
108
// Constructor function with prototype
109
function Person() {
110
this.firstName = "John";
111
this.lastName = "Doe";
112
}
113
Person.prototype.fullName = function() {
114
return this.firstName + " " + this.lastName;
115
};
116
117
const person = new Person();
118
toPairs(person);
119
// => [['firstName', 'John'], ['lastName', 'Doe']]
120
// Note: fullName is not included (prototype property)
121
122
// String conversion
123
toPairs("hello");
124
// => [['0', 'h'], ['1', 'e'], ['2', 'l'], ['3', 'l'], ['4', 'o']]
125
126
// Array-like object
127
const collection = { 0: "zero", 1: "one", length: 2, name: "collection" };
128
toPairs(collection);
129
// => [['0', 'zero'], ['1', 'one'], ['length', 2], ['name', 'collection']]
130
131
// String object conversion
132
const stringObj = Object("xo");
133
toPairs(stringObj);
134
// => [['0', 'x'], ['1', 'o']]
135
136
// Empty object
137
toPairs({});
138
// => []
139
140
// Null/undefined handling
141
toPairs(null);
142
// => []
143
toPairs(undefined);
144
// => []
145
146
// Object with length property (common test case)
147
const objWithLength = { '0': 'a', '1': 'b', 'length': 2 };
148
toPairs(objWithLength);
149
// => [['0', 'a'], ['1', 'b'], ['length', 2]]
150
```
151
152
**Integration with other lodash functions:**
153
154
```javascript
155
import { toPairs, fromPairs } from "lodash";
156
157
// Round-trip conversion
158
const original = { a: 1, b: 2, c: 3 };
159
const roundTrip = fromPairs(toPairs(original));
160
// roundTrip equals original
161
162
// Transforming object values
163
const prices = { apple: "1.20", banana: "0.50", orange: "2.30" };
164
const numericPrices = fromPairs(
165
toPairs(prices).map(([key, value]) => [key, parseFloat(value)])
166
);
167
// => { apple: 1.2, banana: 0.5, orange: 2.3 }
168
```
169
170
### Related Functions
171
172
Lodash provides a related function for handling inherited properties:
173
174
```javascript { .api }
175
/**
176
* Creates an array of own and inherited enumerable key-value pairs for `object`.
177
*
178
* @static
179
* @memberOf _
180
* @category Object
181
* @param {Object} object The object to query.
182
* @returns {Array} Returns the new array of key-value pairs.
183
*/
184
function toPairsIn(object);
185
```
186
187
**Usage comparison:**
188
189
```javascript
190
import { toPairs, toPairsIn } from "lodash";
191
192
function Foo() {
193
this.a = 1;
194
this.b = 2;
195
}
196
Foo.prototype.c = 3;
197
198
const obj = new Foo();
199
200
toPairs(obj); // => [['a', 1], ['b', 2]]
201
toPairsIn(obj); // => [['a', 1], ['b', 2], ['c', 3]]
202
```
203
204
## Types
205
206
Since this is a JavaScript library, types are represented through JSDoc annotations:
207
208
```javascript { .api }
209
/**
210
* @typedef {Object|string|Array|null|undefined} InputType - Accepted input types
211
* @typedef {[string, any]} KeyValuePair - A key-value pair tuple where key is always string
212
* @typedef {KeyValuePair[]} KeyValuePairs - Array of key-value pairs
213
*/
214
215
/**
216
* Creates an array of own enumerable key-value pairs for object.
217
* @param {InputType} object - The object to query. Supports objects, strings, array-like objects, null, and undefined
218
* @returns {KeyValuePairs} Returns the new array of key-value pairs. Empty array for null/undefined input
219
*/
220
function toPairs(object);
221
222
/**
223
* Creates an array of own and inherited enumerable key-value pairs for object.
224
* @param {InputType} object - The object to query
225
* @returns {KeyValuePairs} Returns the new array of key-value pairs including inherited properties
226
*/
227
function toPairsIn(object);
228
```
229
230
## Performance Notes
231
232
- **Time Complexity**: O(n) where n is the number of own enumerable properties
233
- **Space Complexity**: O(n) for the resulting array
234
- **Iteration Order**: Property enumeration order is not guaranteed across JavaScript engines
235
- **Memory**: Creates a new array without mutating the input object