英文:
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 '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));
}
I want like this.
[
{
"DateOfBirth": "2020/02/16",
"BirthDay": "Friday",
"TotalEventPrice": 55000,
"Data": [
{
"Department": "Branch Operation",
"FullName": "Harry Potter"
},
{
"Department": "Finance",
"FullName": "John Wick"
}
]
},
{
"DateOfBirth": "2020/02/19",
"BirthDay": "Monday",
"TotalEventPrice": 22000,
"Data": [
{
"Department": "Loan",
"FullName": "Solomon Kane"
}
]
}
]
答案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 = <String, dynamic>{};
// group by date
for(var item in data) {
final birthday = item['DateOfBirth'] 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['PriceOfEvent'] as num;
dataItem.add({
"Department": item["Department"],
"FullName": item["FullName"]
});
}
// re-group
groupedList.add({
"DateOfBirth": key,
"BirthDay": groupMap[key].first['BirthDay'],
"TotalEventPrice": total,
"Data": dataItem
});
}
print(jsonEncode(groupedList));
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论