forked from lodash/lodash
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmaxBy.js
40 lines (37 loc) · 989 Bytes
/
maxBy.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
import isSymbol from './isSymbol.js'
/**
* This method is like `max` except that it accepts `iteratee` which is
* invoked for each element in `array` to generate the criterion by which
* the value is ranked. The iteratee is invoked with one argument: (value).
*
* @since 4.0.0
* @category Math
* @param {Array} array The array to iterate over.
* @param {Function} iteratee The iteratee invoked per element.
* @returns {*} Returns the maximum value.
* @example
*
* const objects = [{ 'n': 1 }, { 'n': 2 }]
*
* maxBy(objects, ({ n }) => n)
* // => { 'n': 2 }
*/
function maxBy(array, iteratee) {
let result
if (array == null) {
return result
}
let computed
for (const value of array) {
const current = iteratee(value)
if (current != null && (computed === undefined
? (current === current && !isSymbol(current))
: (current > computed)
)) {
computed = current
result = value
}
}
return result
}
export default maxBy