meanBy.js 879 B

1234567891011121314151617181920212223242526272829303132
  1. var baseIteratee = require('./_baseIteratee'),
  2. baseMean = require('./_baseMean');
  3. /**
  4. * This method is like `_.mean` except that it accepts `iteratee` which is
  5. * invoked for each element in `array` to generate the value to be averaged.
  6. * The iteratee is invoked with one argument: (value).
  7. *
  8. * @static
  9. * @memberOf _
  10. * @since 4.7.0
  11. * @category Math
  12. * @param {Array} array The array to iterate over.
  13. * @param {Function} [iteratee=_.identity] The iteratee invoked per element.
  14. * @returns {number} Returns the mean.
  15. * @example
  16. *
  17. * var objects = [{ 'n': 4 }, { 'n': 2 }, { 'n': 8 }, { 'n': 6 }];
  18. *
  19. * _.meanBy(objects, function(o) { return o.n; });
  20. * // => 5
  21. *
  22. * // The `_.property` iteratee shorthand.
  23. * _.meanBy(objects, 'n');
  24. * // => 5
  25. */
  26. function meanBy(array, iteratee) {
  27. return baseMean(array, baseIteratee(iteratee, 2));
  28. }
  29. module.exports = meanBy;