Add _.zipObject.

Former-commit-id: 0fe17adc359fbc608025dced32f6dd509d019413
This commit is contained in:
John-David Dalton
2012-06-29 20:41:12 -04:00
parent 1228639103
commit 4cf2e83418
6 changed files with 132 additions and 63 deletions

View File

@@ -1751,10 +1751,10 @@
}
/**
* Merges together the values of each of the arrays with the value at the
* corresponding position. Useful for separate data sources that are coordinated
* through matching array indexes. For a matrix of nested arrays, `_.zip.apply(...)`
* can transpose the matrix in a similar fashion.
* Merges the elements of each array at their corresponding indexes. Useful for
* separate data sources that are coordinated through matching array indexes.
* For a matrix of nested arrays, `_.zip.apply(...)` can transpose the matrix
* in a similar fashion.
*
* @static
* @memberOf _
@@ -1780,6 +1780,36 @@
return result;
}
/**
* Merges an array of `keys` and an array of `values` into a single object.
*
* @static
* @memberOf _
* @category Arrays
* @param {Array} keys The array of keys.
* @param {Array} [values=[]] The array of values.
* @returns {Object} Returns an object composed of the given keys and
* corresponding values.
* @example
*
* _.zipObject(['moe', 'larry', 'curly'], [30, 40, 50]);
* // => { 'moe': 30, 'larry': 40, 'curly': 50 }
*/
function zipObject(keys, values) {
if (!keys) {
return {};
}
var index = -1,
length = keys.length,
result = {};
values || (values = []);
while (++index < length) {
result[keys[index]] = values[index];
}
return result;
}
/*--------------------------------------------------------------------------*/
/**
@@ -3469,6 +3499,7 @@
lodash.without = without;
lodash.wrap = wrap;
lodash.zip = zip;
lodash.zipObject = zipObject;
// assign aliases
lodash.all = every;