如何获取总活动价格?

huangapple go评论58阅读模式
英文:

dart:I How to get Total Event Price?

问题

这是您要翻译的代码部分的中文翻译:

import 'dart:convert';
void main() {
  final data = [
   {
      "FullName":"Harry Potter",
      "DateOfBirth": "2020/02/16",
      "Department":"Branch Operation",
      "BirthDay":"Friday",
      "PriceOfEvent":35000,
   },
   {
      "FullName":"John Wick",
      "DateOfBirth": "2020/02/16",
      "Department":"Finance",
      "BirthDay":"Friday",
      "PriceOfEvent":20000,
   },
   {
      "FullName":"Solomon Kane",
      "DateOfBirth":"2020/02/19",
      "Department":"Loan",
      "BirthDay":"Monday",
      "PriceOfEvent":22000,
   }
];

double total = 0;
 final value = data.fold(<String, List<dynamic>>{}, (Map<String, List<dynamic>> a, b) {
   a.putIfAbsent(b['DateOfBirth'].toString(), () => []).add(b);
   return a;
 }).values
   .where((l) => l.isNotEmpty)
   .map((l) => {
     'DateOfBirth': l.first['DateOfBirth'],
     'BirthDay': l.first['BirthDay'],
     "TotalEventPrice":total+=l.first["PriceOfEvent"],
     'Data': l.map((e) => {
       'Department': e['Department'],
       'FullName': e['FullName'],
     }).toList()
 }).toList();

  print(json.encode(value));
}

希望这有帮助!

英文:

Here I have GroupBy data but I want Total price of event.
How to total this eventPrice?
Please help me, Thank you?

import &#39;dart:convert&#39;;
void main() {
  final data = [
   {
      &quot;FullName&quot;:&quot;Harry Potter&quot;,
      &quot;DateOfBirth&quot;: &quot;2020/02/16&quot;,
      &quot;Department&quot;:&quot;Branch Operation&quot;,
      &quot;BirthDay&quot;:&quot;Friday&quot;,
      &quot;PriceOfEvent&quot;:35000,
   },
   {
      &quot;FullName&quot;:&quot;John Wick&quot;,
      &quot;DateOfBirth&quot;: &quot;2020/02/16&quot;,
      &quot;Department&quot;:&quot;Finance&quot;,
      &quot;BirthDay&quot;:&quot;Friday&quot;,
      &quot;PriceOfEvent&quot;:20000,
   },
   {
      &quot;FullName&quot;:&quot;Solomon Kane&quot;,
      &quot;DateOfBirth&quot;:&quot;2020/02/19&quot;,
      &quot;Department&quot;:&quot;Loan&quot;,
      &quot;BirthDay&quot;:&quot;Monday&quot;,
      &quot;PriceOfEvent&quot;:22000,
   }
];

double total = 0;
 final value = data.fold(&lt;String, List&lt;dynamic&gt;&gt;{}, (Map&lt;String, List&lt;dynamic&gt;&gt; a, b) {
   a.putIfAbsent(b[&#39;DateOfBirth&#39;].toString(), () =&gt; []).add(b);
   return a;
 }).values
   .where((l) =&gt; l.isNotEmpty)
   .map((l) =&gt; {
     &#39;DateOfBirth&#39;: l.first[&#39;DateOfBirth&#39;],
     &#39;BirthDay&#39;: l.first[&#39;BirthDay&#39;],
     &quot;TotalEventPrice&quot;:total+=l.first[&quot;PriceOfEvent&quot;],
     &#39;Data&#39;: l.map((e) =&gt; {
       &#39;Department&#39;: e[&#39;Department&#39;],
       &#39;FullName&#39;: e[&#39;FullName&#39;],
     }).toList()
 }).toList();

  print(json.encode(value));
}

I want like this.

[
  {
    &quot;DateOfBirth&quot;: &quot;2020/02/16&quot;,
    &quot;BirthDay&quot;: &quot;Friday&quot;,
    &quot;TotalEventPrice&quot;: 55000,
    &quot;Data&quot;: [
      {
        &quot;Department&quot;: &quot;Branch Operation&quot;,
        &quot;FullName&quot;: &quot;Harry Potter&quot;
      },
      {
        &quot;Department&quot;: &quot;Finance&quot;,
        &quot;FullName&quot;: &quot;John Wick&quot;
      }
    ]
  },
  {
    &quot;DateOfBirth&quot;: &quot;2020/02/19&quot;,
    &quot;BirthDay&quot;: &quot;Monday&quot;,
    &quot;TotalEventPrice&quot;: 22000,
    &quot;Data&quot;: [
      {
        &quot;Department&quot;: &quot;Loan&quot;,
        &quot;FullName&quot;: &quot;Solomon Kane&quot;
      }
    ]
  }
]

答案1

得分: 2

这是您的解决方案:

final groupMap = <String, dynamic>{};

// 按日期分组
for (var item in data) {
  final birthday = item['DateOfBirth'] as String;
  if (groupMap.containsKey(birthday)) {
    groupMap[birthday].add(item);
  } else {
    groupMap[birthday] = [item];
  }
}

final groupedList = [];

// 遍历每个日期
for (var key in groupMap.keys) {
  num total = 0;
  final dataItem = [];

  // 计算总事件价格
  for (var item in groupMap[key]) {
    total += item['PriceOfEvent'] as num;
    dataItem.add({
      "Department": item["Department"],
      "FullName": item["FullName"]
    });
  }

  // 重新分组
  groupedList.add({
    "DateOfBirth": key,
    "BirthDay": groupMap[key].first['BirthDay'],
    "TotalEventPrice": total,
    "Data": dataItem
  });
}

print(jsonEncode(groupedList));

请注意,上述代码是对您提供的代码进行的翻译,没有进行任何修改。如果您需要进一步的帮助或有其他问题,请随时告诉我。

英文:

Here is your solution

  final groupMap = &lt;String, dynamic&gt;{};
  
  // group by date
  for(var item in data) {
    final birthday = item[&#39;DateOfBirth&#39;] as String;
    if(groupMap.containsKey(birthday)) {
      groupMap[birthday].add(item);
    } else {
      groupMap[birthday] = [item];
    }
  }
  
  // groupMap
  // {2020/02/16: [{FullName: Harry Potter, DateOfBirth: 2020/02/16, Department: Branch Operation, BirthDay: Friday, PriceOfEvent: 35000}, {FullName: John Wick, DateOfBirth: 2020/02/16, Department: Finance, BirthDay: Friday, PriceOfEvent: 20000}], 2020/02/19: [{FullName: Solomon Kane, DateOfBirth: 2020/02/19, Department: Loan, BirthDay: Monday, PriceOfEvent: 22000}]}
  
  final groupedList = [];
  
  // iterate each date
  for(var key in groupMap.keys) {
    num total = 0;
    final dataItem = [];
    
    // calculate total event price
    for(var item in groupMap[key]) {
      total += item[&#39;PriceOfEvent&#39;] as num;
      dataItem.add({
        &quot;Department&quot;: item[&quot;Department&quot;],
        &quot;FullName&quot;: item[&quot;FullName&quot;]
      });
    }
    
    // re-group
    groupedList.add({
      &quot;DateOfBirth&quot;: key,
      &quot;BirthDay&quot;: groupMap[key].first[&#39;BirthDay&#39;],
      &quot;TotalEventPrice&quot;: total,
      &quot;Data&quot;: dataItem
    });
  }
  
  print(jsonEncode(groupedList));

huangapple
  • 本文由 发表于 2023年7月10日 23:14:21
  • 转载请务必保留本文链接:https://go.coder-hub.com/76655115.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定