8
0

arrayReduce.js 759 B

12345678910111213141516171819202122232425
  1. /**
  2. * A specialized version of `_.reduce` for arrays without support for
  3. * iteratee shorthands.
  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} [initAccum] Specify using the first element of `array` as the initial value.
  10. * @returns {*} Returns the accumulated value.
  11. */
  12. function arrayReduce(array, iteratee, accumulator, initAccum) {
  13. var index = -1,
  14. length = array.length;
  15. if (initAccum && length) {
  16. accumulator = array[++index];
  17. }
  18. while (++index < length) {
  19. accumulator = iteratee(accumulator, array[index], index, array);
  20. }
  21. return accumulator;
  22. }
  23. export default arrayReduce;