如何找到数组中每个类别的总和?
How to find sum for each category in an array?
我有一个包含多个项目的数组。这是一个领域数组。它包含以下结构中的数据:
Results<Entry> <0x7ff04840e1a0> (
[0] Entry {
name = Salary;
amount = 40000;
date = 2020-03-18 16:00:00 +0000;
category = Main Incomes;
entryType = Income;
},
[1] Entry {
name = Diff;
amount = -500;
date = 2020-04-18 16:00:00 +0000;
category = Misc;
entryType = Expense;
},
[2] Entry {
name = Cat Food;
amount = -399;
date = 2020-04-18 16:00:00 +0000;
category = Animals;
entryType = Expense;
},
[3] Entry {
name = Fish Food;
amount = -599;
date = 2020-04-18 16:00:00 +0000;
category = Animals;
entryType = Expense;
}
)
我想要实现的是制作另一个数组,每个类别的总计 'pivot'。所以它可以在 Excel.
中作为支点 table
所需的输出是一个包含每个类别总计的数组:
[0] X-Array {
category = Main Incomes;
amount = XXXX;
},
[1] X-Array {
category = Animals;
amount = XXXX;
And so on...
我对像 .map
和 .reduce
以及其他 Swift 的数组管理糖这样花哨的单行代码还很陌生,非常感谢您的建议!
谢谢!
P.S。我打算对总支出和收入做同样的事情,以计算期末余额。
我认为使用字典更容易进行分类,稍后您可以将其转换为数组或任何您想要的
var dict:[String:Double] = [:]
list.forEach {
if let current = dict[[=10=].category]{
dict[[=10=].category] = current + [=10=].amount
}else{
dict[[=10=].category] = [=10=].amount
}
}
print(dict)
我认为问题是询问如何获取每个类别(收入、费用等)的总和。如果是这样,下面是如何使用结果上的 .sum 函数完成的。
let totalIncome = realm.objects(AccountClass.self)
.filter("entryType == 'Income'")
.sum(ofProperty: "amount") as Int
let totalExpense = realm.objects(AccountClass.self)
.filter("entryType == 'Expense'")
.sum(ofProperty: "amount") as Int
print("Total Income: \(totalIncome)")
print("Total Expense: \(totalExpense)")
您的示例数据的输出是
Total Income: 40000
Total Expense: 1498
如果你想要一个数据透视表 table,你可以将金额添加到数组中 - 如果你需要标签,使用元组将它们存储在这样的数组中
let i = ("Income", totalIncome)
let e = ("Expense", totalExpense)
let tupleArray = [i, e]
for account in tupleArray {
print(account.0, account.1)
}
请记住,Results 对象不是数组,而是具有一些类似数组的函数。结果是实时更新的,因此当 Realm 中的基础数据发生变化时,结果也会随之变化。
我有一个包含多个项目的数组。这是一个领域数组。它包含以下结构中的数据:
Results<Entry> <0x7ff04840e1a0> (
[0] Entry {
name = Salary;
amount = 40000;
date = 2020-03-18 16:00:00 +0000;
category = Main Incomes;
entryType = Income;
},
[1] Entry {
name = Diff;
amount = -500;
date = 2020-04-18 16:00:00 +0000;
category = Misc;
entryType = Expense;
},
[2] Entry {
name = Cat Food;
amount = -399;
date = 2020-04-18 16:00:00 +0000;
category = Animals;
entryType = Expense;
},
[3] Entry {
name = Fish Food;
amount = -599;
date = 2020-04-18 16:00:00 +0000;
category = Animals;
entryType = Expense;
}
)
我想要实现的是制作另一个数组,每个类别的总计 'pivot'。所以它可以在 Excel.
中作为支点 table所需的输出是一个包含每个类别总计的数组:
[0] X-Array {
category = Main Incomes;
amount = XXXX;
},
[1] X-Array {
category = Animals;
amount = XXXX;
And so on...
我对像 .map
和 .reduce
以及其他 Swift 的数组管理糖这样花哨的单行代码还很陌生,非常感谢您的建议!
谢谢!
P.S。我打算对总支出和收入做同样的事情,以计算期末余额。
我认为使用字典更容易进行分类,稍后您可以将其转换为数组或任何您想要的
var dict:[String:Double] = [:]
list.forEach {
if let current = dict[[=10=].category]{
dict[[=10=].category] = current + [=10=].amount
}else{
dict[[=10=].category] = [=10=].amount
}
}
print(dict)
我认为问题是询问如何获取每个类别(收入、费用等)的总和。如果是这样,下面是如何使用结果上的 .sum 函数完成的。
let totalIncome = realm.objects(AccountClass.self)
.filter("entryType == 'Income'")
.sum(ofProperty: "amount") as Int
let totalExpense = realm.objects(AccountClass.self)
.filter("entryType == 'Expense'")
.sum(ofProperty: "amount") as Int
print("Total Income: \(totalIncome)")
print("Total Expense: \(totalExpense)")
您的示例数据的输出是
Total Income: 40000
Total Expense: 1498
如果你想要一个数据透视表 table,你可以将金额添加到数组中 - 如果你需要标签,使用元组将它们存储在这样的数组中
let i = ("Income", totalIncome)
let e = ("Expense", totalExpense)
let tupleArray = [i, e]
for account in tupleArray {
print(account.0, account.1)
}
请记住,Results 对象不是数组,而是具有一些类似数组的函数。结果是实时更新的,因此当 Realm 中的基础数据发生变化时,结果也会随之变化。