arrayReduce.js 796 B

1234567891011121314151617181920212223242526
  1. /**
  2. * A specialized version of `_.reduce` for arrays without support for callback
  3. * shorthands and `this` binding.
  4. *
  5. * @private
  6. * @param {Array} array The array to iterate over.
  7. * @param {Function} iteratee The function invoked per iteration.
  8. * @param {*} [accumulator] The initial value.
  9. * @param {boolean} [initFromArray] Specify using the first element of `array`
  10. * as the initial value.
  11. * @returns {*} Returns the accumulated value.
  12. */
  13. function arrayReduce(array, iteratee, accumulator, initFromArray) {
  14. var index = -1,
  15. length = array.length;
  16. if (initFromArray && length) {
  17. accumulator = array[++index];
  18. }
  19. while (++index < length) {
  20. accumulator = iteratee(accumulator, array[index], index, array);
  21. }
  22. return accumulator;
  23. }
  24. module.exports = arrayReduce;