maxBy.js 1012 B

1234567891011121314151617181920212223242526272829303132333435
  1. var baseExtremum = require('./_baseExtremum'),
  2. baseGt = require('./_baseGt'),
  3. baseIteratee = require('./_baseIteratee');
  4. /**
  5. * This method is like `_.max` except that it accepts `iteratee` which is
  6. * invoked for each element in `array` to generate the criterion by which
  7. * the value is ranked. The iteratee is invoked with one argument: (value).
  8. *
  9. * @static
  10. * @memberOf _
  11. * @since 4.0.0
  12. * @category Math
  13. * @param {Array} array The array to iterate over.
  14. * @param {Array|Function|Object|string} [iteratee=_.identity]
  15. * The iteratee invoked per element.
  16. * @returns {*} Returns the maximum value.
  17. * @example
  18. *
  19. * var objects = [{ 'n': 1 }, { 'n': 2 }];
  20. *
  21. * _.maxBy(objects, function(o) { return o.n; });
  22. * // => { 'n': 2 }
  23. *
  24. * // The `_.property` iteratee shorthand.
  25. * _.maxBy(objects, 'n');
  26. * // => { 'n': 2 }
  27. */
  28. function maxBy(array, iteratee) {
  29. return (array && array.length)
  30. ? baseExtremum(array, baseIteratee(iteratee), baseGt)
  31. : undefined;
  32. }
  33. module.exports = maxBy;