【问题标题】:How to calculate the sum for the year angular 4如何计算年份角 4 的总和
【发布时间】: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


    【解决方案1】:

    如果我正确理解你的英语,你可以扩展你的 reduce 函数来检查日期。

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

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

    【讨论】:

    • 我将编辑我的问题以更容易理解,但我需要计算 1 月、2 月和 3 月月份的总和,当我节省成本时,它有预算和日期以便计算这三个月的所有款项。我编辑了问题
    • 所以你想在 .map 和 .reduce 之间进行过滤?
    • 你写了一些代码,但你能更明确地说我必须做什么,你会救我吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-11-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-07-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多