Skip to content

Instantly share code, notes, and snippets.

@alexcmgit
Created November 25, 2022 02:56
Show Gist options
  • Save alexcmgit/f490ef8b22ce77a6fc0aae1686b7f324 to your computer and use it in GitHub Desktop.
Save alexcmgit/f490ef8b22ce77a6fc0aae1686b7f324 to your computer and use it in GitHub Desktop.
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