How to calculate the sum for the year angular 4

2019-08-10 11:33发布

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

1条回答
男人必须洒脱
2楼-- · 2019-08-10 11:55

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);
查看更多
登录 后发表回答