使用两个字段将数组添加到对象

时间:2017-12-22 12:13:00

标签: javascript arrays typescript

我有一个数组chartData,它有两个值。在"系列"我希望它能够保存一个我可以推送三个对象的数组。我这样做是为了使用ngxcharts制作热图。但是,我真的不知道如何将新数组分配到"系列"。这是我一直在尝试的事情:

prepareArray(): void
  {
    for(var item of this.transactions)
    {
      let index = this.chartData.findIndex(x => x.name == item.shopperCountryCode);
      if(index === -1)
      {
        this.chartData.push({
           "name": item.shopperCountryCode,
            "series": [] as any[]
        });

          this.chartData.series.push({
             "name": "16-34",
              "value": 0
          });

          this.chartData.series.push({
             "name": "35-50",
              "value": 0
           });

          this.chartData.series.push({
             "name": "51-77",
              "value": 0
          });
      }
      else
      {
        this.age = new Date().getFullYear() - new Date(item.dateOfBirth).getFullYear();
        if(this.age <= 34)
        {
          let subIndex = this.chartData[index].series.findIndex(x => x.name == "16-34");
          this.chartData[index].series[subIndex] = this.chartData[index].series[subIndex].value++;
        }
        if(this.age > 34 && this.age <= 50)
        {
          let subIndex = this.chartData[index].series.findIndex(x => x.name == "35-50");
          this.chartData[index].series[subIndex] = this.chartData[index].series[subIndex].value++;
        }
        if(this.age > 50)
        {
          let subIndex = this.chartData[index].series.findIndex(x => x.name == "51-77");
          this.chartData[index].series[subIndex] = this.chartData[index].series[subIndex].value++;
        }
      }
    }
  }

0 个答案:

没有答案