始终返回数组Javascript中的值数,

时间:2018-04-08 03:50:34

标签: javascript json vue-chartjs

美好的一天,

我有这样的JSON数据,

const incidents = [
   {
      "id": 3,
      "fullName": "Fatima Elaran",
      "address": "San Marcos, Calumpit, Bulacan",
   },
   {
      "id": 4,
      "fullName": "Leni Elaran",
      "address": "San Marcos, Calumpit, Bulacan",
   }
]

我按地址汇总总数,方法如下:

      const count = incidents.reduce((a, c) => {
        a[c.address] = (a[c.address] || 0) + 1;
        return a;
      }, {});

count设置为:

{ San Marcos, Calumpit, Bulacan: 2 }

现在,我更新我的图表以显示此汇总数据:

      Object.keys(count).forEach(key => {
        this.barChart.labels.push(key);
      });
      Object.values(count).forEach(data => {
        this.barChart.data.push(data);
      });

barChart.data以此数据结尾:[2, "San Marcos, Calumpit, Bulacan"]

我希望它只有这个:["San Marcos, Calumpit, Bulacan"]

0 个答案:

没有答案