rekey<K1> method

  1. @useResult
Map<K1, V> rekey<K1>(
  1. K1 convert(
    1. K key
    )
)

Returns a copy of this map with its keys transformed using convert.

Contract

convert should always return distinct keys. Earlier entries may be replaced by later entries with the same key otherwise.

Example

final foo = {1: 1, 2: 2};
final bar = foo.rekey((k) => '$k'); // {'1': 1, '2': 2}

See revalue for converting values and map for converting entries.

Implementation

@useResult Map<K1, V> rekey<K1>(K1 Function(K key) convert) => {
  for (final MapEntry(:key, :value) in entries)
    convert(key): value,
};