Flutter/Dart how to groupBy list of maps
The package collection implements
the groupBy
function.
For grouping by date:
import "package:collection/collection.dart";
main(List<String> args) {
var data = [
{"title": 'Avengers', "release_date": '10/01/2019'},
{"title": 'Creed', "release_date": '10/01/2019'},
{"title": 'Jumanji', "release_date": '30/10/2019'},
];
var newMap = groupBy(data, (Map obj) => obj['release_date']);
print(newMap);
}
For removing the release_date
key from each map entry:
var newMap = groupBy(data, (Map obj) => obj['release_date']).map(
(k, v) => MapEntry(k, v.map((item) { item.remove('release_date'); return item;}).toList()));
For changing a key:
var newMap = groupBy(data, (Map obj) => obj['release_date']).map(
(k, v) => MapEntry(k, v.map((item) => {'name': item['title']}).toList()));
If you have Dart 2.7, you can extend Iterable
to add a useful groupBy
method:
extension Iterables<E> on Iterable<E> {
Map<K, List<E>> groupBy<K>(K Function(E) keyFunction) => fold(
<K, List<E>>{},
(Map<K, List<E>> map, E element) =>
map..putIfAbsent(keyFunction(element), () => <E>[]).add(element));
}
Now, you're List of Maps, could be grouped using something like:
final releaseDateMap = listOfMaps.groupBy((m) => m['release_date'])
Data like this:
[
{title: 'Avengers', release_date: '10/01/2019'},
{title: 'Creed', release_date: '10/01/2019'}
{title: 'Jumanji', release_date: '30/10/2019'},
]
would turn into:
{
'10/01/2019': [
{title: 'Avengers', release_date: '10/01/2019'},
{title: 'Creed', release_date: '10/01/2019'}
],
'30/10/2019': [
{title: 'Jumanji', release_date: '30/10/2019'},
]
}
This is a method naively implemented (in case you don't want to use the groupBy function from the collections package):
List<Map<String, List<Map<String, String>>>> MapByKey(String keyName, String newKeyName, String keyForNewName, List<Map<String,String>> input) {
Map<String, Map<String, List<Map<String, String>>>> returnValue = Map<String, Map<String, List<Map<String, String>>>>();
for (var currMap in input) {
if (currMap.containsKey(keyName)) {
var currKeyValue = currMap[keyName];
var currKeyValueForNewName = currMap[keyForNewName];
if (!returnValue.containsKey(currKeyValue)){
returnValue[currKeyValue] = {currKeyValue : List<Map<String, String>>()};
}
returnValue[currKeyValue][currKeyValue].add({newKeyName : currKeyValueForNewName});
}
}
return returnValue.values.toList();
}
void main() {
var test = [
{"title": 'Avengers', "release_date": '10/01/2019'},
{"title": 'Creed', "release_date": '10/01/2019'},
{"title": 'Jumanji', "release_date": '30/10/2019'},
];
var testMapped = MapByKey("release_date", "name", "title", test);
print("$testMapped");
}
The output is:
[
{
10/01/2019: [
{name: Avengers
},
{name: Creed
}
]
},
{
30/10/2019: [
{name: Jumanji
}
]
}
]
Using the supercharged package, you'd write it like this:
List list = [
{ title: 'Avengers', release_date: '10/01/2019' },
{ title: 'Creed', release_date: '10/01/2019' }
{ title: 'Jumanji', release_date: '30/10/2019' },
];
final map = list.groupBy<String, Map>((item) =>
item['release_date'],
valueTransform: (item) => item..remove('release_date'),
);
To add to the accepted answer if you come arcross this, In flutter 2, you will Get an error, as i got.
The operator '[]' isn't defined for the type 'dynamic Function(dynamic)'
Use
var data = [
{"title": 'Avengers', "release_date": '10/01/2019'},
{"title": 'Creed', "release_date": '10/01/2019'},
{"title": 'Jumanji', "release_date": '30/10/2019'},
];
var newMap = groupBy(data, (Map oj) => oj['release_date']);
print(newMap);
This might be of help to someone.