Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download
80742 views
1
var isArray = require('../lang/isArray');
2
3
/**
4
* The inverse of `_.pairs`; this method returns an object composed from arrays
5
* of property names and values. Provide either a single two dimensional array,
6
* e.g. `[[key1, value1], [key2, value2]]` or two arrays, one of property names
7
* and one of corresponding values.
8
*
9
* @static
10
* @memberOf _
11
* @alias object
12
* @category Array
13
* @param {Array} props The property names.
14
* @param {Array} [values=[]] The property values.
15
* @returns {Object} Returns the new object.
16
* @example
17
*
18
* _.zipObject([['fred', 30], ['barney', 40]]);
19
* // => { 'fred': 30, 'barney': 40 }
20
*
21
* _.zipObject(['fred', 'barney'], [30, 40]);
22
* // => { 'fred': 30, 'barney': 40 }
23
*/
24
function zipObject(props, values) {
25
var index = -1,
26
length = props ? props.length : 0,
27
result = {};
28
29
if (length && !values && !isArray(props[0])) {
30
values = [];
31
}
32
while (++index < length) {
33
var key = props[index];
34
if (values) {
35
result[key] = values[index];
36
} else if (key) {
37
result[key[0]] = key[1];
38
}
39
}
40
return result;
41
}
42
43
module.exports = zipObject;
44
45