mirror of
https://github.com/whoisclebs/lodash.git
synced 2026-02-01 15:57:48 +00:00
Bump to v3.0.0.
This commit is contained in:
39
array/zipObject.js
Normal file
39
array/zipObject.js
Normal file
@@ -0,0 +1,39 @@
|
||||
import isArray from '../lang/isArray';
|
||||
|
||||
/**
|
||||
* Creates an object composed from arrays of property names and values. Provide
|
||||
* either a single two dimensional array, e.g. `[[key1, value1], [key2, value2]]`
|
||||
* or two arrays, one of property names and one of corresponding values.
|
||||
*
|
||||
* @static
|
||||
* @memberOf _
|
||||
* @alias object
|
||||
* @category Array
|
||||
* @param {Array} props The property names.
|
||||
* @param {Array} [values=[]] The property values.
|
||||
* @returns {Object} Returns the new object.
|
||||
* @example
|
||||
*
|
||||
* _.zipObject(['fred', 'barney'], [30, 40]);
|
||||
* // => { 'fred': 30, 'barney': 40 }
|
||||
*/
|
||||
function zipObject(props, values) {
|
||||
var index = -1,
|
||||
length = props ? props.length : 0,
|
||||
result = {};
|
||||
|
||||
if (length && !values && !isArray(props[0])) {
|
||||
values = [];
|
||||
}
|
||||
while (++index < length) {
|
||||
var key = props[index];
|
||||
if (values) {
|
||||
result[key] = values[index];
|
||||
} else if (key) {
|
||||
result[key[0]] = key[1];
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
export default zipObject;
|
||||
Reference in New Issue
Block a user