zipWith.js 1.2 KB

123456789101112131415161718192021222324252627282930313233343536
  1. var restParam = require('../function/restParam'),
  2. unzipWith = require('./unzipWith');
  3. /**
  4. * This method is like `_.zip` except that it accepts an iteratee to specify
  5. * how grouped values should be combined. The `iteratee` is bound to `thisArg`
  6. * and invoked with four arguments: (accumulator, value, index, group).
  7. *
  8. * @static
  9. * @memberOf _
  10. * @category Array
  11. * @param {...Array} [arrays] The arrays to process.
  12. * @param {Function} [iteratee] The function to combine grouped values.
  13. * @param {*} [thisArg] The `this` binding of `iteratee`.
  14. * @returns {Array} Returns the new array of grouped elements.
  15. * @example
  16. *
  17. * _.zipWith([1, 2], [10, 20], [100, 200], _.add);
  18. * // => [111, 222]
  19. */
  20. var zipWith = restParam(function(arrays) {
  21. var length = arrays.length,
  22. iteratee = length > 2 ? arrays[length - 2] : undefined,
  23. thisArg = length > 1 ? arrays[length - 1] : undefined;
  24. if (length > 2 && typeof iteratee == 'function') {
  25. length -= 2;
  26. } else {
  27. iteratee = (length > 1 && typeof thisArg == 'function') ? (--length, thisArg) : undefined;
  28. thisArg = undefined;
  29. }
  30. arrays.length = length;
  31. return unzipWith(arrays, iteratee, thisArg);
  32. });
  33. module.exports = zipWith;