keyBy<K, V> static method

Map<V, Map<K, V>> keyBy<K, V>(
  1. Iterable<Map<K, V>> list,
  2. K key
)

Indexes list by the value at key. Entries with a null value at key are skipped.

Arr.keyBy([{'id': 1}, {'id': 2}], 'id'); // {1: {id: 1}, 2: {id: 2}}

Implementation

static Map<V, Map<K, V>> keyBy<K, V>(Iterable<Map<K, V>> list, K key) {
  final result = <V, Map<K, V>>{};
  for (final m in list) {
    final k = m[key];
    if (k != null) result[k] = m;
  }
  return result;
}