Created
November 25, 2022 02:56
-
-
Save alexcmgit/f490ef8b22ce77a6fc0aae1686b7f324 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
void main() { | |
List tagId = ['a', 'b']; | |
List<Map<String, dynamic>> oldList = [ | |
{'id': 'a', 'value': 1}, | |
{'id': 'a', 'value': 1}, | |
{'id': 'a', 'value': 1}, | |
{'id': 'a', 'value': 1}, | |
{'id': 'b', 'value': 1}, | |
{'id': 'b', 'value': 1} | |
]; | |
final merged = ([{}] + oldList).reduce( | |
(map, e) => map..[e['id']] = (map[e['id']] ?? 0) + (e['value'] ?? 0), | |
); | |
var mergedAsList = | |
merged.keys.map((key) => {'id': key, 'value': merged[key]}); | |
print(mergedAsList); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment