or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

docs

index.md
tile.json

tessl/npm-lodash--frompairs

The lodash method fromPairs exported as a standalone module for converting key-value pairs to objects

Workspace
tessl
Visibility
Public
Created
Last updated
Describes
npmpkg:npm/lodash.frompairs@4.0.x

To install, run

npx @tessl/cli install tessl/npm-lodash--frompairs@4.0.0

index.mddocs/

Lodash fromPairs

Lodash fromPairs is a standalone modularized version of the Lodash fromPairs utility function. It provides a lightweight way to convert an array of key-value pairs into a JavaScript object without requiring the entire Lodash library.

Package Information

  • Package Name: lodash.frompairs
  • Package Type: npm
  • Language: JavaScript
  • Installation: npm install lodash.frompairs

Core Imports

var fromPairs = require('lodash.frompairs');

For ES6 modules (if your environment supports it):

import fromPairs from 'lodash.frompairs';

Basic Usage

var fromPairs = require('lodash.frompairs');

// Convert array of key-value pairs to object
var pairs = [['fred', 30], ['barney', 40]];
var result = fromPairs(pairs);
// => { 'fred': 30, 'barney': 40 }

// Empty array returns empty object
var empty = fromPairs([]);
// => {}

// Handles nested values
var nested = [['user', { name: 'Alice', age: 25 }], ['active', true]];
var obj = fromPairs(nested);
// => { 'user': { name: 'Alice', age: 25 }, 'active': true }

Capabilities

Object Creation from Pairs

Converts an array of key-value pairs into a JavaScript object. This is the inverse operation of converting an object to pairs.

/**
 * The inverse of _.toPairs; this method returns an object composed from key-value pairs.
 * 
 * @param {Array} pairs - The key-value pairs where each element is a two-element array [key, value]
 * @returns {Object} Returns the new object composed from the key-value pairs
 */
function fromPairs(pairs);

Parameters:

  • pairs (Array): Array of key-value pairs. Each element should be a two-element array where the first element is the property key and the second element is the property value.

Returns:

  • (Object): New object with properties created from the key-value pairs.

Behavior:

  • If pairs is falsy (null, undefined), returns an empty object {}
  • Keys are treated literally - no deep path creation (e.g., 'a.b.c' becomes a single property key)
  • If a pair doesn't have a second element, the value will be undefined
  • Duplicate keys will be overwritten by the last occurrence
  • Works with any data types as keys (converted to strings) and values

Usage Examples:

var fromPairs = require('lodash.frompairs');

// Basic usage
fromPairs([['a', 1], ['b', 2]]);
// => { 'a': 1, 'b': 2 }

// Mixed data types
fromPairs([['name', 'Alice'], ['age', 30], ['active', true]]);
// => { 'name': 'Alice', 'age': 30, 'active': true }

// Keys with dots are treated literally (not as nested paths)
fromPairs([['user.name', 'Bob'], ['user.age', 25]]);
// => { 'user.name': 'Bob', 'user.age': 25 }

// Handles falsy input gracefully
fromPairs(null);
// => {}

fromPairs(undefined);
// => {}

// Empty array
fromPairs([]);
// => {}

// Incomplete pairs (missing values become undefined)
fromPairs([['key1'], ['key2', 'value2']]);
// => { 'key1': undefined, 'key2': 'value2' }

// Complex values
fromPairs([
  ['config', { theme: 'dark', debug: true }],
  ['handlers', [function() {}, function() {}]],
  ['metadata', new Date()]
]);
// => {
//   'config': { theme: 'dark', debug: true },
//   'handlers': [function() {}, function() {}],
//   'metadata': [Date object]
// }

Error Handling

The function is designed to be fault-tolerant:

  • Falsy input: Returns empty object {} for null, undefined, or other falsy values
  • Malformed pairs: If pairs don't contain two-element arrays, missing values default to undefined
  • No exceptions: The function does not throw errors for invalid input

Related Functions

This function is the inverse of toPairs from the main Lodash library:

// If using both lodash.frompairs and lodash.topairs
var toPairs = require('lodash.topairs');
var fromPairs = require('lodash.frompairs');

var obj = { 'a': 1, 'b': 2 };
var pairs = toPairs(obj);  // [['a', 1], ['b', 2]]
var restored = fromPairs(pairs);  // { 'a': 1, 'b': 2 }

Performance Considerations

  • Lightweight: Zero dependencies, minimal overhead
  • Memory efficient: Creates new object without modifying input array
  • Time complexity: O(n) where n is the number of pairs
  • Suitable for: Data transformation, API response processing, configuration object creation