0

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??

4

1 回答 1

0

我已经使用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方法。

于 2021-09-24T09:14:11.867 回答