【问题标题】:Remove duplicate value in dart [closed]删除飞镖中的重复值[关闭]
【发布时间】:2019-06-02 21:54:39
【问题描述】:

我是 dart and flutter 的新手。我尝试了任何javascript代码来解决它,但它不起作用:(。

我想获得订单的长度,但我不想从destination.id 获得相同的值。这个案子有什么想法吗?是否可以使用 library queries.dart 或任何东西来解决这种情况?

这是我的 JSON 数据。

{
    "data": {
        "shipments": [
            {
                "id": "1608",
                "orders": [
                    {
                        "destination": {
                            "id": "979"
                        }
                    },
                    {
                        "destination": {
                            "id": "979"
                        }
                    },
                    {
                        "destination": {
                            "id": "1074"
                        }
                    },
                    {
                        "destination": {
                            "id": "1074"
                        }
                    },
                    {
                        "destination": {
                            "id": "1022"
                        }
                    },
                    {
                        "destination": {
                            "id": "1022"
                        }
                    },
                ]
            },
            {
                "id": "1599",
                "orders": [
                    {
                        "destination": {
                            "id": "979"
                        }
                    },
                    {
                        "destination": {
                            "id": "979"
                        }
                    },
                    {
                        "destination": {
                            "id": "1660"
                        }
                    },
                    {
                        "destination": {
                            "id": "1660"
                        }
                    },
                ]
            }
        ]
    }
}

【问题讨论】:

    标签: json dart flutter


    【解决方案1】:

    试试:

    var data = ... your JSON data ...;
    List orders = data["data"]["shipments"]["orders"];
    var uniqueIds = orders.map((o) => o["destination"]["id"]).toSet();
    var uniqueCount = uniqueIds.length;
    

    这会在 Set 中收集唯一 ID,从而确保忽略重复项并对其进行计数。

    【讨论】:

    • 谢谢老兄,它的工作原理:D 我在 map() 中添加了 if 语句,就像这样 => if (o["destination"] == null) { return 0; } else { return o["destination"]["id"]; } 清楚吗?
    • 如果“订单”中的某些元素没有目的地(示例表明它们都有),这是有道理的。记得之后从集合中删除0
    【解决方案2】:

    您还可以使用集合包中的函数 groupBy。它将以 id 为键整齐地收集列表映射中的所有重复项。

    import 'package:collection/collection.dart';
    
    List orders = data["data"]["shipments"]["orders"];
    var newMap = groupBy(orders, (obj) => obj['destination']['id']);
    var length = newMap.length;
    print("$newMap, length: $length");
    

    输出:

    {
        979: [
            {destination: {id: 979
                }
            },
            {destination: {id: 979
                }
            }
        ],
        1074: [
            {destination: {id: 1074
                }
            },
            {destination: {id: 1074
                }
            }
        ],
        1022: [
            {destination: {id: 1022
                }
            },
            {destination: {id: 1022
                }
            }
        ]
    },
    length: 3
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-11-14
      • 2012-08-15
      • 1970-01-01
      • 1970-01-01
      • 2023-01-09
      • 2019-03-17
      • 2021-05-26
      • 2018-10-29
      相关资源
      最近更新 更多