[Lodash的.maxBy与JavaScript等效吗?我在minimax的YouTube教程中看到了代码_.maxBy(values, ...)
。我不想使用任何外部依赖关系,但是当我在搜索问题时,找不到任何能回答我问题的信息。我确实有一个猜测-forEach除外,它还会找到最大值
您可以看到实现here,它是开源的:
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
}