我正在从api中检索数据映射,如下所示
{
"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"
]
}
},
]
我的目标是排序和组他们与关键的"章"。如下所示
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"
]
}
}
与此相似,有多个章节。那么我该如何对它们进行分组,使章节名称排在前面然后章节的所有相关值都排在该组后面?什么是正确的方法来获得这些值
**更新:-我试图使用for循环来迭代数据。我要做的是,如果键存在组相应的数据,否则添加键
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');
}
}
我怎样才能正确地迭代和分组??
我已经使用List.forEach
方法和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);
或者你也可以使用groupBy
方法从收集包。