【发布时间】:2018-10-03 03:52:29
【问题描述】:
我已经创建了彼此之间的总成本,但现在我需要总结一整年的所有预算。 我已经尝试过了,但这不起作用,它显示我未定义。
Costs 是一个接口,它有一个类型为 date 的变量 created 和一个类型为 number 的变量 cost 以及它发送到一个数组列表 actualcostIds 的计划成本的实际成本的 id。 那么如何计算从 1 月 1 日到 12 月 31 日的所有总和 您将在下面找到每个费用和总费用的总和代码。
export interface TotalCosten {
planned: number;
actual: number;
}
export function emptyCosts(): Costs {
return {
id: '',
created: new Date(),
type: 0,
costs: 0,
reference: '',
comment: ''
};
}
export function getTotalYearCosts(valueItem: ValueItem, allCosts: Map<string, Costs>): TotalCosten {
const totalYearCosts = { planned: 0, actual: 0 };
const Q1 = new Date(2018, 11, 31);
const Q2 = new Date(2018, 0, 1);
totalYearCosts.actual = valueItem.actualCostIds
.map(costId => allCosts.get(costId, emptyCosts()).costs)
.filter() //Here to make a filter for costs that are in the date range
.reduce((reduction, costs) => reduction + costs, 0);
return totalYearCosts;
}
export interface ValueItem {
plannedCostIds: string[];
actualCostIds: string[];
}
【问题讨论】:
标签: javascript angular typescript observable