Reputation: 382
I'm retrieving a map of data from api as follows
{
"success": true,
"data": {
"items": [
{
"sequenceno": 1933,
"_id": "5eff1",
"chapter": "Numbers and Numeration",
"title": "Place Value: 3-digits",
"package_description": "This learning module helps to familiarise with the concept of place value of 3-digit numbers.",
"age_level": [
99,
8
],
"pkg_sequence": "2501",
"packagescors": {
"score": [
50
],
"date": [
"1600259121340"
]
}
},
{
"sequenceno": 1933,
"_id": "5d79",
"chapter": "Numbers and Numeration",
"title": "Place Value: 4-digits",
"package_description": "This learning module helps the kids familiarise with the concept of Place value of a number.",
"age_level": [
99,
8
],
"pkg_sequence": "2501",
"packagescors": {
"score": [
60
],
"date": [
"1615283866457"
]
}
},
]
My objective is sort and group them with the key 'Chapter'. as given below
Numbers and Numeration : { // chapter name
"sequenceno": 1933,
"_id": "5d79",
"chapter": "Numbers and Numeration",
"title": "Place Value: 4-digits",
"package_description": "This learning module helps the kids familiarise with the concept of Place value of a number.",
"age_level": [
99,
8
],
"pkg_sequence": "2501",
"packagescors": {
"score": [
60
],
"date": [
"1615283866457"
]
{
"sequenceno": 1933,
"_id": "5eff1",
"chapter": "Numbers and Numeration",
"title": "Place Value: 3-digits",
"package_description": "This learning module helps to familiarise with the concept of place value of 3-digit numbers.",
"age_level": [
99,
8
],
"pkg_sequence": "2501",
"packagescors": {
"score": [
50
],
"date": [
"1600259121340"
]
}
}
Similar to this there are multiple chapters. So how can I group them so that chapter name comes first then all the related values of chapter comes under that group? What is the proper way to get the values as this
** Update : - I'm trying to use for loop to iterate through the data. What i'm trying to do is to if the key exists group the data accordingly else to add the keys
Map<Item,dynamic> sortedTable = {};
Iterable<Map<String, dynamic>> data = subjectModules.map((e) => e.toMap());
for (var map in data) {
try{
if(sortedTable.containsKey(map['chapter'])){ // if key exists
// need to add and data group it
}else{ // if not
sortedTable.putIfAbsent(map['chapter'], () => map); // i need add only keys
}
}catch(e){
print('Error $e');
}
}
How can i properly iterate and group it??
Upvotes: 0
Views: 947
Reputation: 1150
I have created list of groups by chapter name using List.forEach
method and Map.putIfAbsent
.
final resp = {
"data": {
"items": [
{
"sequenceno": 1933,
"_id": "5eff1",
"chapter": "Numbers and Numeration",
"title": "Place Value: 3-digits",
"package_description":
"This learning module helps to familiarise with the concept of place value of 3-digit numbers.",
"age_level": [99, 8],
"pkg_sequence": "2501",
"packagescors": {
"score": [50],
"date": ["1600259121340"]
}
},
{
"sequenceno": 1933,
"_id": "5d79",
"chapter": "Numbers and Numeration",
"title": "Place Value: 4-digits",
"package_description":
"This learning module helps the kids familiarise with the concept of Place value of a number.",
"age_level": [99, 8],
"pkg_sequence": "2501",
"packagescors": {
"score": [60],
"date": ["1615283866457"]
}
},
{
"sequenceno": 1933,
"_id": "5d79",
"chapter": "Numbers Numeration",
"title": "Place Value: 4-digits",
"package_description":
"This learning module helps the kids familiarise with the concept of Place value of a number.",
"age_level": [99, 8],
"pkg_sequence": "2501",
"packagescors": {
"score": [60],
"date": ["1615283866457"]
}
},
]
}
};
final ans = {};
resp["data"]?["items"]?.forEach((e) {
final entry = ans.putIfAbsent(e["chapter"], () => []);
entry.add(e);
});
print(ans);
alternatively you can also use groupBy
method from collection package.
Upvotes: 0