Return Ember JS 中当月的费用总和
Return sum of expense on current month in Ember JS
我想获得我的 支出总和,但按 当前月份 筛选。
expenses: computed('transactions.length', 'transactions.@each.amount', function() {
return this.get('transactions').filterBy('typeOfT','expense').sortBy('date')
}),
filteredExpenses: computed('expenses.length', 'expenses.@each.amount', function() {
let thisMonth = new Date().getFullYear()+'-'+(new Date().getMonth()+1)
return this.get('expenses').filterBy('date', 'thisMonth').mapBy('amount').reduce((a, b) => a + b, 0)
}),
所以尝试 filterBy('date', 'thisMonth')
函数没有用。我认为这会更容易,但事实并非如此。使用 mapBy('amount')
和 reduce((a, b) => a + b, 0)
我可以获得所有费用的数组,并使用该函数计算总和。
我的模特:
export default Model.extend({
category: DS.attr(),
name: DS.attr(),
description: DS.attr(),
amount: DS.attr(),
date: DS.attr(),
typeOfT: DS.attr(),
user: DS.belongsTo('user')
});
我不是 100% 过滤月份(可能需要一些调整),但总的来说这应该会给你当月的费用:
import { computed } from '@ember/object';
import { filterBy, mapBy, sum } from '@ember/object/computed';
// first get all of your expenses in an array by filtering on `typeOfT`:
expenses: filterBy('transactions', 'typeOfT', 'expense'),
// then filter expenses by the current month
currentMonthExpenses: computed('expenses', function() {
return this.get('expenses').filter(expense => {
return new Date(expense.get('date')).getMonth() === new Date().getMonth();
});
}),
// now map all of this month's expenses by their amounts:
currentMonthExpenseAmounts: mapBy('currentMonthExpenses', 'amount'),
// now sum all of the current month's expense amounts:
sumOfCurrentMonthExpenses: sum('currentMonthExpensesAmounts')
我想获得我的 支出总和,但按 当前月份 筛选。
expenses: computed('transactions.length', 'transactions.@each.amount', function() {
return this.get('transactions').filterBy('typeOfT','expense').sortBy('date')
}),
filteredExpenses: computed('expenses.length', 'expenses.@each.amount', function() {
let thisMonth = new Date().getFullYear()+'-'+(new Date().getMonth()+1)
return this.get('expenses').filterBy('date', 'thisMonth').mapBy('amount').reduce((a, b) => a + b, 0)
}),
所以尝试 filterBy('date', 'thisMonth')
函数没有用。我认为这会更容易,但事实并非如此。使用 mapBy('amount')
和 reduce((a, b) => a + b, 0)
我可以获得所有费用的数组,并使用该函数计算总和。
我的模特:
export default Model.extend({
category: DS.attr(),
name: DS.attr(),
description: DS.attr(),
amount: DS.attr(),
date: DS.attr(),
typeOfT: DS.attr(),
user: DS.belongsTo('user')
});
我不是 100% 过滤月份(可能需要一些调整),但总的来说这应该会给你当月的费用:
import { computed } from '@ember/object';
import { filterBy, mapBy, sum } from '@ember/object/computed';
// first get all of your expenses in an array by filtering on `typeOfT`:
expenses: filterBy('transactions', 'typeOfT', 'expense'),
// then filter expenses by the current month
currentMonthExpenses: computed('expenses', function() {
return this.get('expenses').filter(expense => {
return new Date(expense.get('date')).getMonth() === new Date().getMonth();
});
}),
// now map all of this month's expenses by their amounts:
currentMonthExpenseAmounts: mapBy('currentMonthExpenses', 'amount'),
// now sum all of the current month's expense amounts:
sumOfCurrentMonthExpenses: sum('currentMonthExpensesAmounts')