# maxBy
# Description
maxBy 的作用是找出一组数据中的最大值。和 Math.max 不同的是,maxBy 支持传入一个迭代器 iteratee ,在比较的过程中不直接比较原始值,而是使用迭代器返回的值进行比较。
# Params
(array, iteratee)
# Return
{*}
# Depend
import isSymbol from './isSymbol.js'
# Code
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
}
# Analyze
首先定义了结果
result,没有赋值针对
array是null或者undefined进行了处理,直接返回了result,也就是undefined定义
computed作为上一轮较大的值for...of循环进行比较,这里if判断条件略复杂, 拆开来看current != null首先第一点,
current也就是当前值不能为null或者undefined,如果为这两个值,则完全没有比较的意义computed === undefined ? (current === current && !isSymbol(current)) : (current > computed)其次,如果上一次比较的结果
computed为undefined,则表示是第一次进行比较,或者之前没有对computed进行过 赋值current === current && !isSymbol(current)此时则判断
current === current,表示current不能为NaN,然后current也不能为symbol类型,在满足这两点时,会进行第一次对computed的赋值操作如果
computed已经有值的情况下current > computed这里判断的是
current大于computed,也就是说,只有在大于时,才需要进行值的更新computed = current result = value在满足条件时,进行赋值,更新
computed和result的值最终在遍历完成后,返回
result结果即可
# Remark
- for...of MDN (opens new window) 语句在可迭代对象(包括 Array,Map,Set,String,TypedArray,arguments 对象等等)上创建一个迭代循环,调用自定义迭代钩子,并为每个不同属性的值执行语句
# Example
console.log(maxBy([3,6,2,8,1,9,2,67,4,9], (v) => v)) // 67
← matchesProperty mean →