如何计算角度4的年的总和 [英] How to calculate the sum for the year angular 4

查看:83
本文介绍了如何计算角度4的年的总和的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了彼此之间的费用总和,但是现在我需要对整个年度的所有预算进行总和. 我已经尝试过了,但是这不起作用,显示给我的是不确定的.

I have created to sum the costs between each other but now I need to sum all the budget for complete year. I have tried this but this is not working it is showing me undefined.

Costs是一个接口,该接口具有一个创建为date类型的变量,一个类型为number的变量cost以及要发送到数组的计划成本的实际成本id,该接口列出了costedIds. 那么如何计算从1月1日到12月31日的所有总和 在下面,您将找到我的代码,表示每个成本和总成本的总和.

Costs is an interface that has a variable created of type date and the variable costs of type number and the id of the actual costs of planned costs it is sending to an array lists actualcostIds. So how it is possible to calculate all the sum from 1 January to 31 December Below you will find my code for sum for every each costs and total summed costs.

  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[];
}

推荐答案

如果我正确地理解了您的英语,则可以扩展reduce函数来检查日期.

If i understand your english correctly, you can expand your reduce function to check the date.

那么如何根据日期计算所有总和

So how it is possible to calculate all the sum depending the date

.reduce((reduction, costs) => {
    if (checkDate) {
        return reduction + costs
    } else {
        return 0
    }
}, 0);

这篇关于如何计算角度4的年的总和的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆