删除飞镖中的重复值 [英] Remove duplicate value in dart

查看:80
本文介绍了删除飞镖中的重复值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是飞镖和飞镖的新手.我已经尝试过使用javascript的任何代码来解决该问题,但这是行不通的:(.

I'm the newbie in dart and flutter. I have try any code of javascript to solve it, but it's doesn't work:(.

我想获取订单长度,但是我不想从destination.id获取相同的值.对这种情况有什么想法吗?可以使用库query.dart或其他任何方法来解决这种情况吗?

I want get length of orders, but I don't want get the same value from the destination.id. Any idea for this case? Is possible to use library queries.dart or anything for solve this case?

这是我的JSON数据.

This is my JSON data.

{
    "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;

这会将唯一ID收集到Set中,以确保重复项被忽略并计数.

This collects the unique IDs in a Set, which ensures that duplicates are ignored, and counts them.

这篇关于删除飞镖中的重复值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆