30秒学会 Dart 片段 · 2019年3月24日

30秒学会 Dart 片段 – minBy

Returns the minimum value of a list, after mapping each element to a number using the provided function.

Use Iterable.map() to map each element to the numeric value returned by fn, min() to find the minimum value.

代码实现

import 'dart:math';

num minBy<T>(List<T> lst, num Function(T) fn) {
  return lst.map(fn).reduce(math.min);
}

使用样例

minBy([ {'n': 4}, {'n': 2}, {'n': 8}, {'n': 6} ], (o) => o['n']); // 8