Creates an array of own enumerable key-value pairs for object
npx @tessl/cli install tessl/npm-lodash--topairs@4.3.0The 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.
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.
npm install lodashimport { toPairs } from "lodash";For related functionality:
import { toPairs, toPairsIn } from "lodash";For CommonJS:
const { toPairs } = require("lodash");const { toPairs, toPairsIn } = require("lodash");Full lodash import:
import _ from "lodash";
// Use as _.toPairs() and _.toPairsIn()const _ = require("lodash");
// Use as _.toPairs() and _.toPairsIn()import { toPairs } from "lodash";
// Basic object conversion
const obj = { a: 1, b: 2, c: 3 };
const pairs = toPairs(obj);
// => [['a', 1], ['b', 2], ['c', 3]]
// String handling
const str = "ab";
const stringPairs = toPairs(str);
// => [['0', 'a'], ['1', 'b']]
// Object with length property
const arrayLike = { '0': 'first', '1': 'second', 'length': 2 };
const arrayLikePairs = toPairs(arrayLike);
// => [['0', 'first'], ['1', 'second'], ['length', 2]]Converts an object's own enumerable properties into an array of [key, value] pairs.
/**
* Creates an array of own enumerable key-value pairs for `object`.
*
* @static
* @memberOf _
* @category Object
* @param {Object|string|null|undefined} object The object to query.
* @returns {Array} Returns the new array of key-value pairs.
*/
function toPairs(object);Behavior:
Usage Examples:
import { toPairs } from "lodash";
// Basic object
const user = { name: "Alice", age: 30 };
toPairs(user);
// => [['name', 'Alice'], ['age', 30]]
// Constructor function with prototype
function Person() {
this.firstName = "John";
this.lastName = "Doe";
}
Person.prototype.fullName = function() {
return this.firstName + " " + this.lastName;
};
const person = new Person();
toPairs(person);
// => [['firstName', 'John'], ['lastName', 'Doe']]
// Note: fullName is not included (prototype property)
// String conversion
toPairs("hello");
// => [['0', 'h'], ['1', 'e'], ['2', 'l'], ['3', 'l'], ['4', 'o']]
// Array-like object
const collection = { 0: "zero", 1: "one", length: 2, name: "collection" };
toPairs(collection);
// => [['0', 'zero'], ['1', 'one'], ['length', 2], ['name', 'collection']]
// String object conversion
const stringObj = Object("xo");
toPairs(stringObj);
// => [['0', 'x'], ['1', 'o']]
// Empty object
toPairs({});
// => []
// Null/undefined handling
toPairs(null);
// => []
toPairs(undefined);
// => []
// Object with length property (common test case)
const objWithLength = { '0': 'a', '1': 'b', 'length': 2 };
toPairs(objWithLength);
// => [['0', 'a'], ['1', 'b'], ['length', 2]]Integration with other lodash functions:
import { toPairs, fromPairs } from "lodash";
// Round-trip conversion
const original = { a: 1, b: 2, c: 3 };
const roundTrip = fromPairs(toPairs(original));
// roundTrip equals original
// Transforming object values
const prices = { apple: "1.20", banana: "0.50", orange: "2.30" };
const numericPrices = fromPairs(
toPairs(prices).map(([key, value]) => [key, parseFloat(value)])
);
// => { apple: 1.2, banana: 0.5, orange: 2.3 }Lodash provides a related function for handling inherited properties:
/**
* Creates an array of own and inherited enumerable key-value pairs for `object`.
*
* @static
* @memberOf _
* @category Object
* @param {Object} object The object to query.
* @returns {Array} Returns the new array of key-value pairs.
*/
function toPairsIn(object);Usage comparison:
import { toPairs, toPairsIn } from "lodash";
function Foo() {
this.a = 1;
this.b = 2;
}
Foo.prototype.c = 3;
const obj = new Foo();
toPairs(obj); // => [['a', 1], ['b', 2]]
toPairsIn(obj); // => [['a', 1], ['b', 2], ['c', 3]]Since this is a JavaScript library, types are represented through JSDoc annotations:
/**
* @typedef {Object|string|Array|null|undefined} InputType - Accepted input types
* @typedef {[string, any]} KeyValuePair - A key-value pair tuple where key is always string
* @typedef {KeyValuePair[]} KeyValuePairs - Array of key-value pairs
*/
/**
* Creates an array of own enumerable key-value pairs for object.
* @param {InputType} object - The object to query. Supports objects, strings, array-like objects, null, and undefined
* @returns {KeyValuePairs} Returns the new array of key-value pairs. Empty array for null/undefined input
*/
function toPairs(object);
/**
* Creates an array of own and inherited enumerable key-value pairs for object.
* @param {InputType} object - The object to query
* @returns {KeyValuePairs} Returns the new array of key-value pairs including inherited properties
*/
function toPairsIn(object);