0

再会,

我有一个这样的 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"]

4

0 回答 0