Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove duplicate value in dart [closed]

Tags:

json

flutter

dart

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

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?

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"
                        }
                    },
                ]
            }
        ]
    }
}
like image 437
user10883017 Avatar asked Feb 23 '26 14:02

user10883017


1 Answers

Try:

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

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

like image 75
lrn Avatar answered Feb 26 '26 09:02

lrn



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!