【问题标题】:Flutter how to sort and group a map of dataFlutter 如何对数据图进行排序和分组
【发布时间】:2023-03-18 09:09:01
【问题描述】:

我正在从 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');
      }
    }

如何正确迭代和分组?

【问题讨论】:

    标签: flutter for-loop dart fluttermap


    【解决方案1】:

    我使用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);
    

    您也可以使用collection 包中的groupBy 方法。

    【讨论】:

    • 嘿 @ryg-git ,我一直在尝试使用 for 循环。我正在尝试在将存储传递到地图之前对数据进行分组。有什么方法可以让我们先将密钥传递给地图?我也在更新我的问题
    • 根据我从您对问题的更新中了解到的情况,我认为您希望将组存储在地图中,而不是像我在回答中所做的那样,如果是这种情况,我认为您应该选择可以使用唯一键将数据存储为映射中的值,例如_id,并且可能在else 子句sortedTable.putIfAbsent(map['chapter'], () =&gt; {map['_id']: map});if 子句sortedTable[map['chapter']][map['_id']] = map 中使用它,但我认为将List 用于组也是一种好主意。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-11-20
    • 2021-03-30
    • 2022-07-07
    • 2013-12-19
    • 2021-03-21
    相关资源
    最近更新 更多