Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download
80742 views
1
var arrayMap = require('../internal/arrayMap'),
2
arrayReduce = require('../internal/arrayReduce'),
3
bindCallback = require('../internal/bindCallback'),
4
unzip = require('./unzip');
5
6
/**
7
* This method is like `_.unzip` except that it accepts an iteratee to specify
8
* how regrouped values should be combined. The `iteratee` is bound to `thisArg`
9
* and invoked with four arguments: (accumulator, value, index, group).
10
*
11
* @static
12
* @memberOf _
13
* @category Array
14
* @param {Array} array The array of grouped elements to process.
15
* @param {Function} [iteratee] The function to combine regrouped values.
16
* @param {*} [thisArg] The `this` binding of `iteratee`.
17
* @returns {Array} Returns the new array of regrouped elements.
18
* @example
19
*
20
* var zipped = _.zip([1, 2], [10, 20], [100, 200]);
21
* // => [[1, 10, 100], [2, 20, 200]]
22
*
23
* _.unzipWith(zipped, _.add);
24
* // => [3, 30, 300]
25
*/
26
function unzipWith(array, iteratee, thisArg) {
27
var length = array ? array.length : 0;
28
if (!length) {
29
return [];
30
}
31
var result = unzip(array);
32
if (iteratee == null) {
33
return result;
34
}
35
iteratee = bindCallback(iteratee, thisArg, 4);
36
return arrayMap(result, function(group) {
37
return arrayReduce(group, iteratee, undefined, true);
38
});
39
}
40
41
module.exports = unzipWith;
42
43