wanted result
I want each log on the same page to be separated and type as “food” and “work” instead of [food, work, food] so that I can calculate how much I spent on each type of expenses.
My logs are like this
In file 2022-09-21:
#spend
Amount:: 20
Info:: restaurant A
Type:: food
Amount:: 40
Info:: book A
Type:: work
Amount:: 15
Info:: restaurant B
Type:: food
In file 2022-09-22:
#spend
Amount:: 22
Info:: restaurant C
Type:: food
Amount:: 30
Info:: book B
Type:: work
Amount:: 17
Info:: restaurant D
Type:: food
In file 2022-09-20
#spend
Amount:: 11
Type:: test
My attempts
dataview version
code
TABLE
sum(sum(number(rows.Amount))) AS amount,
typeof(rows.type),
rows.type,
rows.type[0],
length(Rows.type[0])
FROM #spend
GROUP BY type
result
problem
Type is treated as [“food”, “work”, “food”] and “test”, but I want “food”, “work” and “test”.
dataviewjs attempts
code
const data = dv.pages("#spend")
for(let type of data.type) {
var type_sum = 0
for(let data_item of data) {
for (let j =0; j <data_item.type.length; j++){
if(data_item.type[j] == type) { type_sum += data_item.amount[j]; }
}
}
dv.paragraph(type + ' costs: $'+ type_sum);
}
result
problem
- It iterates over the type array so there are redundant outputs, I only want each type to be iterated once. To get unique elements in an array js can do
const unique_eles = [...new Set(data.map(item => item.group))];
but dataviewjs seemed not to accept this. - For 1 element array (“test”) the amount is wrong, still remains 0.