I have a list of data that looks like that:

{
  "wallet": {...},
  "transactions": [
    {
      "createdAd": 1617121058,
      "id": 1,
      "value": 100
      ...
    },
    {
      "createdAd": 1617121090,
      "id": 1,
      "value": 100
      ...
    },
   ...
  ]
}

Now I need to group the transactions by date ( all transactions on the same day not same second ) to be something like below:

{
  "wallet": {...},
  "transactions": [
    {
      "createdAd": 1617121058,
      "transactions": [
        // list of all transactions that have been done at that day ( not time )
      ]

    },
    {
      "createdAd": 1617121058, // another day
      "transactions": [
        // list of all transactions that have been done at that day ( not time )
      ]

    },

   // ...etc
  ]
}

I don't know if I should put some code here but I don't guess it will that useful as my code most of it SQLite queries and mapping so will kinda unrelated to what I need here.


Solution 1: ibrahimxcool

Try converting the unix time to a readable date, something like this

 var transDate = DateTime.fromMillisecondsSinceEpoch(1617121058*1000);

And then you can play with it.. if(transDate.day == 30)...


Solution 2: Andrija

Try this. This could be further optimized - if your data set is too large; and code can be made smaller, but it should work like this.

Just don't remove cascade operator (..) here Map.from(element)..remove("createdAd") - it won't work without it

You can try this quickly in https://dartpad.dev/

DateTime toDateTime(int d) {
  var dt=DateTime.fromMillisecondsSinceEpoch(d);
  return DateTime(dt.year, dt.month, dt.day);
}

void main() {
  var input = <String, dynamic>{
    "wallet": {"a": 1},
    "transactions": [
      {"createdAd": 1617121058, "id": 1, "value": 100},
      {"createdAd": 1617121058, "id": 2, "value": 200},
      {"createdAd": 1617121090, "id": 1, "value": 100},
      {"createdAd": 1617121090, "id": 2, "value": 400},
      {"createdAd": 2619021099, "id": 1, "value": 100},
      {"createdAd": 2619021099, "id": 2, "value": 600},
    ]
  };

  // DateTime.fromMillisecondsSinceEpoch(1617121058*1000)
  // get the unique dates
  var dates =
      input["transactions"].map((element) => toDateTime(element["createdAd"])).toSet();

  print(dates);

  var transactions = <Map<String, dynamic>>[];

  // loop through all dates
  for (var d in dates) {
    var tranOnDate = (input["transactions"] as List<Map<String, dynamic>>)
        .where((element) => toDateTime(element["createdAd"]) == d)
        .map((element) => Map.from(element)..remove("createdAd"))
        .toList();

    transactions.add({"createdAd": d, "transactions": tranOnDate});
  }

  // final output
  var output = <String, dynamic>{
    "wallet": input["wallet"],
    "transactions": transactions
  };

  print(output);
}


Solution 3: Ramkumar.M

The package collection implements the groupBy function.

import "package:collection/collection.dart";
var groupedArray = groupBy(data, (obj) => DateTime.fromMillisecondsSinceEpoch(obj['createdAd']*1000));