The lodash method fromPairs exported as a standalone module for converting key-value pairs to objects
npx @tessl/cli install tessl/npm-lodash--frompairs@4.0.0Lodash 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.
npm install lodash.frompairsvar fromPairs = require('lodash.frompairs');For ES6 modules (if your environment supports it):
import fromPairs from 'lodash.frompairs';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 }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:
Behavior:
pairs is falsy (null, undefined), returns an empty object {}undefinedUsage 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]
// }The function is designed to be fault-tolerant:
{} for null, undefined, or other falsy valuesundefinedThis 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 }