如何有效使用角度数组/列表

时间:2018-06-21 05:20:33

标签: arrays angular list

因此,我正在为网站构建分析。我正在从rest API中提取数据,并将其输入到有角度的前端。

我的.component.ts文件中有类似这样的数组:
     数组1

this.statsPassed24Hrs = [
{'Description': 'Total A', 'Count': this.a},
{'Description': 'Total B', 'Count': this.b},
{'Description': 'Total C', 'Count': this.c},
{'Description': 'Total D', 'Count': this.d},
{'Description': 'Total E', 'Count': this.e}
];

阵列2

this.statsPassed7Days = [
{'Description': 'Total A', 'Count': this.aPassed7Days},
{'Description': 'Total B', 'Count': this.bPassed7Days},
{'Description': 'Total C', 'Count': this.cPassed7Days},
{'Description': 'Total D', 'Count': this.dPassed7Days},
{'Description': 'Total E', 'Count': this.ePassed7Days}
];

数组3

this.statsPassedMonth = [
  {'Description': 'Total A', 'Count': this.aPassedMonth},
  {'Description': 'Total B', 'Count': this.bPassedMonth},
  {'Description': 'Total C', 'Count': this.cPassedMonth},
  {'Description': 'Total D', 'Count': this.dPassedMonth},
  {'Description': 'Total E', 'Count': this.ePassedMonth}
];

正如您在所有数组中看到的一样,'Description'保持不变,而'Count'是唯一改变的东西。

我希望能够使用一个数组来操纵它。我只是认为创建十几个列表不是一个好的解决方案

任何想法或指导将不胜感激。

感谢

1 个答案:

答案 0 :(得分:1)

在数组中添加一个属性,如下所示:

this.statsPassedMonth = [
  {'Description': 'Total A', 'CountMonth': this.aPassedMonth, 'Count24Hrs': this.a, 'Count7Days':this.aPassed7Days},
  {'Description': 'Total B', 'CountMonth': this.bPassedMonth, 'Count24Hrs': this.b, 'Count7Days':this.bPassed7Days},
  {'Description': 'Total C', 'CountMonth': this.cPassedMonth, 'Count24Hrs': this.c, 'Count7Days':this.cPassed7Days},
  {'Description': 'Total D', 'CountMonth': this.dPassedMonth, 'Count24Hrs': this.d, 'Count7Days':this.dPassed7Days},
  {'Description': 'Total E', 'CountMonth': this.ePassedMonth, 'Count24Hrs': this.e, 'Count7Days':this.ePassed7Days}
];
相关问题