打字稿:按月和年分组数组

时间:2020-03-27 08:52:13

标签: arrays typescript date

我目前有一个包含对象的数组,这些对象具有一个score:Number和date:Date。我需要在图表中显示这些分数,并按月和年分组。

图表的标签应该与本示例类似:

public barChartLabels: Label[] = [ '01/2019', '02/2019', '03/2019', '04/2019', '05/2019', '06/2019', '07/2019' ];

我可以正确显示标签,但是不确定如何显示按月/年分组的分数。 EmployeeResults是一个{score:Number,creationDate:Date}对象的数组

      this.barChartLabels = this.result.employeeResults.map(emp => {
        const date = new Date(emp.creationDate);
        const month = date.getMonth() + 1;
        const year = date.getFullYear();
        return `${month.toString()}/${year.toString()}`;
      });

      this.barChartData = [
        //replace with real scores
        { data: [65, 59, 80, 81, 56, 55, 40], label: this.result.company },
        { data: [28, 48, ], label: 'Industry Average' }
      ];

0 个答案:

没有答案