unzipWith.js 1.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. var apply = require('./_apply'),
  2. arrayMap = require('./_arrayMap'),
  3. unzip = require('./unzip');
  4. /**
  5. * This method is like `_.unzip` except that it accepts `iteratee` to specify
  6. * how regrouped values should be combined. The iteratee is invoked with the
  7. * elements of each group: (...group).
  8. *
  9. * @static
  10. * @memberOf _
  11. * @since 3.8.0
  12. * @category Array
  13. * @param {Array} array The array of grouped elements to process.
  14. * @param {Function} [iteratee=_.identity] The function to combine
  15. * regrouped values.
  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) {
  26. if (!(array && array.length)) {
  27. return [];
  28. }
  29. var result = unzip(array);
  30. if (iteratee == null) {
  31. return result;
  32. }
  33. return arrayMap(result, function(group) {
  34. return apply(iteratee, undefined, group);
  35. });
  36. }
  37. module.exports = unzipWith;