The lodash zipObject array utility function for creating objects from arrays of keys and values
npx @tessl/cli install tessl/npm-lodash-zipobject@4.1.00
# lodash.zipobject
1
2
The lodash zipObject array utility function as a standalone Node.js module. This package provides the `zipObject` function that creates objects by combining two arrays - one containing property names and another containing corresponding values. This method is like `_.fromPairs` except that it accepts two arrays instead of a single array of pairs.
3
4
## Package Information
5
6
- **Package Name**: lodash.zipobject
7
- **Package Type**: npm
8
- **Language**: JavaScript
9
- **Installation**: `npm install lodash.zipobject`
10
11
## Core Imports
12
13
```javascript
14
var zipObject = require('lodash.zipobject');
15
```
16
17
For ES modules (if using a bundler that supports it):
18
19
```javascript
20
import zipObject from 'lodash.zipobject';
21
```
22
23
## Basic Usage
24
25
```javascript
26
var zipObject = require('lodash.zipobject');
27
28
// Create an object from arrays of keys and values
29
var keys = ['a', 'b', 'c'];
30
var values = [1, 2, 3];
31
var result = zipObject(keys, values);
32
// => { 'a': 1, 'b': 2, 'c': 3 }
33
34
// Handle mismatched array lengths
35
var result2 = zipObject(['x', 'y', 'z'], [10, 20]);
36
// => { 'x': 10, 'y': 20, 'z': undefined }
37
38
// Handle empty or null inputs
39
var result3 = zipObject(null, [1, 2, 3]);
40
// => {}
41
42
var result4 = zipObject(['a', 'b'], null);
43
// => { 'a': undefined, 'b': undefined }
44
```
45
46
## Capabilities
47
48
### Object Creation from Arrays
49
50
Creates an object by combining two arrays - one of property identifiers and one of corresponding values. Available since lodash version 0.4.0.
51
52
```javascript { .api }
53
/**
54
* Creates an object composed from arrays of property names and values.
55
* @static
56
* @memberOf _
57
* @since 0.4.0
58
* @category Array
59
* @param {Array} [props=[]] The property identifiers.
60
* @param {Array} [values=[]] The property values.
61
* @returns {Object} Returns the new object.
62
*/
63
function zipObject(props, values);
64
```
65
66
**Parameters:**
67
- `props` (Array, optional): Array of property identifiers/keys. Defaults to empty array if not provided or null/undefined
68
- `values` (Array, optional): Array of property values. Defaults to empty array if not provided or null/undefined
69
70
**Returns:**
71
- `Object`: A new object with properties created from combining the props and values arrays
72
73
**Behavior:**
74
- When arrays have different lengths, properties without corresponding values are assigned `undefined`
75
- Null or undefined input arrays are treated as empty arrays
76
- The function creates a new object and does not modify the input arrays
77
- Uses SameValueZero equality comparison internally for property assignment
78
- Supports any valid JavaScript property names (strings) and any JavaScript values
79
80
**Usage Examples:**
81
82
```javascript
83
var zipObject = require('lodash.zipobject');
84
85
// Basic usage
86
zipObject(['a', 'b'], [1, 2]);
87
// => { 'a': 1, 'b': 2 }
88
89
// Mismatched lengths - extra keys get undefined
90
zipObject(['fred', 'barney', 'pebbles'], [30, 40]);
91
// => { 'fred': 30, 'barney': 40, 'pebbles': undefined }
92
93
// Mismatched lengths - extra values are ignored
94
zipObject(['a', 'b'], [1, 2, 3, 4]);
95
// => { 'a': 1, 'b': 2 }
96
97
// Empty arrays
98
zipObject([], []);
99
// => {}
100
101
// Various value types
102
zipObject(['str', 'num', 'bool', 'obj', 'arr'],
103
['hello', 42, true, {x: 1}, [1, 2, 3]]);
104
// => { 'str': 'hello', 'num': 42, 'bool': true, 'obj': {x: 1}, 'arr': [1, 2, 3] }
105
106
// Null/undefined handling
107
zipObject(null, [1, 2, 3]);
108
// => {}
109
110
zipObject(['a', 'b', 'c'], undefined);
111
// => { 'a': undefined, 'b': undefined, 'c': undefined }
112
```