groupBy<K> method

Map<K, List<T>> groupBy<K>(
  1. K key(
    1. T e
    )
)

Groups elements by a computed key.

Example:

users.groupBy((u) => u.role);

Implementation

Map<K, List<T>> groupBy<K>(K Function(T e) key) {
  final map = <K, List<T>>{};
  for (final e in this) {
    map.putIfAbsent(key(e), () => []).add(e);
  }
  return map;
}