or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

Files

docs

index.md
tile.json

index.mddocs/

0

# Lodash fromPairs

1

2

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.

3

4

## Package Information

5

6

- **Package Name**: lodash.frompairs

7

- **Package Type**: npm

8

- **Language**: JavaScript

9

- **Installation**: `npm install lodash.frompairs`

10

11

## Core Imports

12

13

```javascript

14

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

15

```

16

17

For ES6 modules (if your environment supports it):

18

19

```javascript

20

import fromPairs from 'lodash.frompairs';

21

```

22

23

## Basic Usage

24

25

```javascript

26

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

27

28

// Convert array of key-value pairs to object

29

var pairs = [['fred', 30], ['barney', 40]];

30

var result = fromPairs(pairs);

31

// => { 'fred': 30, 'barney': 40 }

32

33

// Empty array returns empty object

34

var empty = fromPairs([]);

35

// => {}

36

37

// Handles nested values

38

var nested = [['user', { name: 'Alice', age: 25 }], ['active', true]];

39

var obj = fromPairs(nested);

40

// => { 'user': { name: 'Alice', age: 25 }, 'active': true }

41

```

42

43

## Capabilities

44

45

### Object Creation from Pairs

46

47

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

48

49

```javascript { .api }

50

/**

51

* The inverse of _.toPairs; this method returns an object composed from key-value pairs.

52

*

53

* @param {Array} pairs - The key-value pairs where each element is a two-element array [key, value]

54

* @returns {Object} Returns the new object composed from the key-value pairs

55

*/

56

function fromPairs(pairs);

57

```

58

59

**Parameters:**

60

- `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.

61

62

**Returns:**

63

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

64

65

**Behavior:**

66

- If `pairs` is falsy (null, undefined), returns an empty object `{}`

67

- Keys are treated literally - no deep path creation (e.g., 'a.b.c' becomes a single property key)

68

- If a pair doesn't have a second element, the value will be `undefined`

69

- Duplicate keys will be overwritten by the last occurrence

70

- Works with any data types as keys (converted to strings) and values

71

72

**Usage Examples:**

73

74

```javascript

75

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

76

77

// Basic usage

78

fromPairs([['a', 1], ['b', 2]]);

79

// => { 'a': 1, 'b': 2 }

80

81

// Mixed data types

82

fromPairs([['name', 'Alice'], ['age', 30], ['active', true]]);

83

// => { 'name': 'Alice', 'age': 30, 'active': true }

84

85

// Keys with dots are treated literally (not as nested paths)

86

fromPairs([['user.name', 'Bob'], ['user.age', 25]]);

87

// => { 'user.name': 'Bob', 'user.age': 25 }

88

89

// Handles falsy input gracefully

90

fromPairs(null);

91

// => {}

92

93

fromPairs(undefined);

94

// => {}

95

96

// Empty array

97

fromPairs([]);

98

// => {}

99

100

// Incomplete pairs (missing values become undefined)

101

fromPairs([['key1'], ['key2', 'value2']]);

102

// => { 'key1': undefined, 'key2': 'value2' }

103

104

// Complex values

105

fromPairs([

106

['config', { theme: 'dark', debug: true }],

107

['handlers', [function() {}, function() {}]],

108

['metadata', new Date()]

109

]);

110

// => {

111

// 'config': { theme: 'dark', debug: true },

112

// 'handlers': [function() {}, function() {}],

113

// 'metadata': [Date object]

114

// }

115

```

116

117

## Error Handling

118

119

The function is designed to be fault-tolerant:

120

121

- **Falsy input**: Returns empty object `{}` for null, undefined, or other falsy values

122

- **Malformed pairs**: If pairs don't contain two-element arrays, missing values default to `undefined`

123

- **No exceptions**: The function does not throw errors for invalid input

124

125

## Related Functions

126

127

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

128

129

```javascript

130

// If using both lodash.frompairs and lodash.topairs

131

var toPairs = require('lodash.topairs');

132

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

133

134

var obj = { 'a': 1, 'b': 2 };

135

var pairs = toPairs(obj); // [['a', 1], ['b', 2]]

136

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

137

```

138

139

## Performance Considerations

140

141

- **Lightweight**: Zero dependencies, minimal overhead

142

- **Memory efficient**: Creates new object without modifying input array

143

- **Time complexity**: O(n) where n is the number of pairs

144

- **Suitable for**: Data transformation, API response processing, configuration object creation