What is the JavaScript equivalent of Lodash’s .maxBy? I saw the code _.maxBy(values, ...)
in a YouTube tutorial on minimax. I didn’t want to use any external dependencies, but when I googled my question, I couldn’t find anything that answered my question.
I do have a guess though - forEach except it also finds the maximum value
Asked
Active
Viewed 623 times
0

shreyasm-dev
- 2,711
- 5
- 16
- 34
1 Answers
1
You can see the implementation here, it's open source:
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
}

Luís Ramalho
- 10,018
- 4
- 52
- 67