zipWith.js 943 B

12345678910111213141516171819202122232425262728293031
  1. var rest = require('./rest'),
  2. unzipWith = require('./unzipWith');
  3. /**
  4. * This method is like `_.zip` except that it accepts `iteratee` to specify
  5. * how grouped values should be combined. The iteratee is invoked with the
  6. * elements of each group: (...group).
  7. *
  8. * @static
  9. * @memberOf _
  10. * @since 3.8.0
  11. * @category Array
  12. * @param {...Array} [arrays] The arrays to process.
  13. * @param {Function} [iteratee=_.identity] The function to combine grouped values.
  14. * @returns {Array} Returns the new array of grouped elements.
  15. * @example
  16. *
  17. * _.zipWith([1, 2], [10, 20], [100, 200], function(a, b, c) {
  18. * return a + b + c;
  19. * });
  20. * // => [111, 222]
  21. */
  22. var zipWith = rest(function(arrays) {
  23. var length = arrays.length,
  24. iteratee = length > 1 ? arrays[length - 1] : undefined;
  25. iteratee = typeof iteratee == 'function' ? (arrays.pop(), iteratee) : undefined;
  26. return unzipWith(arrays, iteratee);
  27. });
  28. module.exports = zipWith;