-
Notifications
You must be signed in to change notification settings - Fork 1.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
add mapValues to Map<K, V> #9590
Comments
This comment was originally written by @seaneagan If we had issue #7088, then could do: var newMap = new Map.fromPairs<K, V>(m.pairs.map((pair) => new Pair(pair.key, f(pair.value)))); and if Map instead directly extended Iterable<Pair<K, V>> it could be: var newMap = new Map.from(m.map((pair) => new Pair(pair.key, f(pair.value)))); |
This comment was originally written by [email protected] Added Area-Library label. |
Added Triaged label. |
This comment was originally written by @seaneagan FWIW I've been using this in the meantime: Map mapValues(Map map, var valueMapper(key)) => map.keys.fold({}, (result, key) { |
This comment was originally written by @seaneagan An alternative would be to add optional factory Map.from(Map other, {K key(key), V value(value)}); |
Issue #21780 has been merged into this issue. |
You can use Map.fromIterables: var mappedMap = new Map.fromIterables(map.keys, map.values.map(transform)); There is a limit to how many specialized transformers we want to add. Another alternative is to create a wrapper. It's not that complex: import "dart:collection"; |
Map.map has landed. |
I often find myself writing
Map newMap = new Map();
m.forEach((k, v) {
newMap[k] = f(v);
});
...
It would be great if Map had a map or mapValues method instead:
Map newMap = m.mapValues(f);
The text was updated successfully, but these errors were encountered: