unzipWith.js 1.3 KB

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