json - 删除 Dart 中的重复值

标签 json dart flutter

<分区>

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

我想要获取订单的长度,但我不想从 destination.id 获取相同的值。对这种情况有什么想法吗?是否可以使用库 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"
                        }
                    },
                ]
            }
        ]
    }
}

最佳答案

尝试:

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,以确保忽略重复项,并对它们进行计数。

关于json - 删除 Dart 中的重复值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54087857/

相关文章:

button - 对齐 appBar 下方的按钮,在 flutter 中没有任何空间

javascript - 如何在不使用 eval 的情况下动态访问 JSON 节点?

angularjs - typings.json 没有更新 typings install --ambient

java - 在保持LogicalTypes的同时将Parquet/Avro GenericRecord写入JSON

android - 为 local_auth Flutter 添加自定义对话框

android - Flutter-启用蓝牙服务的蓝牙程序包

javascript - 使用多个 if 语句过滤 json 输出

Dart polymer : binding html files to scripts results in broken bootstrap. Dart 包含 "%5C"个字符

regex - 检查字符串是否有效,位智实时 map 链接

dart - Dart 控制台应用程序中的退出代码 139 是什么意思?