而动态地把循环通过JSON对象的阵列中的表数据的统计

问题描述 投票:0回答:1

我从含有JSON对象,其中,在JSON数据,属性得分被解释这样的阵列的API动态试图输出统计数据在表中在JavaScript:

分数:5(优秀);

评分:4(非常好);

得分:3(合理的);

得分:2(差);

得分:1(可怕);

现在的挑战是:在数组中循环,在表中添加统计资料。统计数据应立足于shop name及其score计算。例如,在JSON,我们在物业(storeName)为同一家公司在不同的位置和业主想2店分别评估基于客户的反馈各项性能。

在阵列的性能和价值"storeName": "Dito Savassi",我们有4次出现此店(微积分应该是这样的:

totalOfExcellence / totalOccurencesOfThisShop * 100(2/4 * 100),totalOfVeryGood / totalOccurencesOfThisShop * 100; totalOfVeryGood / totalOccurencesOfThisShop * 100;等等)。

你会重复的"storeName": "Dito Rio de Janeiro",其中有一行(1个occurence),并在表中的列totalOfVeryGood = 1;"satisfaction"过程后:(totalOfExcellence + totalOfVeryGood );,并在表中的列"Evaluation"the number of occurence of each shop or total row for each shop

我已经做了所有店铺的报告一起,现在这个情况下,我不知道如何使它发挥作用。不要任何人有任何想法?

[{
    "id": 1,
    "date": "2018-09-01T13:27:57.334Z",
    "storeId": 1,
    "storeName": "Dito Savassi",
    "score": 5
  },
  {
    "id": 2,
    "date": "2018-09-01T13:27:57.334Z",
    "storeId": 2,
    "storeName": "Dito Rio de Janeiro",
    "score": 4
  },
  {
    "id": 3,
    "date": "2018-09-02T13:27:57.334Z",
    "storeId": 1,
    "storeName": "Dito Savassi",
    "score": 5
  },
  {
    "id": 4,
    "date": "2018-09-3T13:27:57.334Z",
    "storeId": 1,
    "storeName": "Dito Savassi",
    "score": 3
  },
  {
    "id": 5,
    "date": "2018-09-03T13:27:57.334Z",
    "storeId": 1,
    "storeName": "Dito Savassi",
    "score": 2
  }
]

结果应该是这样的:

<table class="table">
  <thead>
    <tr>
      <th>Name of the shop</th>
      <th>Satisfaction</th>
      <th>Evaluation</th>
      <th>excellent</th>
      <th>Very Good</th>
      <th>Reasonable</th>
      <th>Bad</th>
      <th>horribel</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>1 Dito Savassi</td>
      <td>50%</td>
      <td>4</td>
      <td>50%</td>
      <td>0%</td>
      <td>25%</td>
      <td>25%</td>
      <td>0%</td>
    </tr>
    <tr>
      <td>2 Dito Rio de Janeiro</td>
      <td>100%</td>
      <td>1</td>
      <td>0%</td>
      <td>100%</td>
      <td>0%</td>
      <td>0%</td>
      <td>0%</td>
    </tr>
  </tbody>
</table>
javascript arrays json
1个回答
0
投票

var jsonData = [{
    "id": 1,
    "date": "2018-09-01T13:27:57.334Z",
    "storeId": 1,
    "storeName": "Dito Savassi",
    "score": 5
  },
  {
    "id": 2,
    "date": "2018-09-01T13:27:57.334Z",
    "storeId": 2,
    "storeName": "Dito Rio de Janeiro",
    "score": 4
  },
  {
    "id": 3,
    "date": "2018-09-02T13:27:57.334Z",
    "storeId": 1,
    "storeName": "Dito Savassi",
    "score": 5
  },
  {
    "id": 4,
    "date": "2018-09-3T13:27:57.334Z",
    "storeId": 1,
    "storeName": "Dito Savassi",
    "score": 3
  },
  {
    "id": 5,
    "date": "2018-09-03T13:27:57.334Z",
    "storeId": 1,
    "storeName": "Dito Savassi",
    "score": 2
  }
];
var tr_data = '';
for (var key in jsonData) {
  //console.log(jsonData[key]);	
  if (jsonData.hasOwnProperty(key)) {
    console.log(jsonData[key].storeName);
    var review = '';
    switch (jsonData[key].score) {
      case 1:
        review = 'horrible';
        break;
      case 2:
        review = 'bad';
        break;
      case 3:
        review = 'Reasonable';
        break;
      case 4:
        review = 'very good';
        break;
      case 5:
        review = 'excellent';
        break;
    }
    tr_data += '<tr>' +
      '<td>' + jsonData[key].id + '</td>' +
      '<td>' + jsonData[key].date + '</td>' +
      '<td>' + jsonData[key].storeId + '</td>' +
      '<td>' + jsonData[key].storeName + '</td>' +
      '<td>' + jsonData[key].score + '</td>' +
      '<td>' + review + '</td>' +
      '</tr>';
  }
}

var htmlTable = '<table cellpadding="10" style="font-family: Tahoma, Geneva, sans-serif;border: 1px solid #1C6EA4;background-color: #EEEEEE;width: 100%;text-align: left;">' +
  '<thead>' +
  '<tr>' +
  '<th>Id</th>' +
  '<th>Date</th>' +
  '<th>Store Id</th>' +
  '<th>Store Name</th>' +
  '<th>Score</th>' +
  '<th>Review</th>' +
  ' </tr>' +
  '</thead>' +
  ' <tbody>' + tr_data + '</tbody>' +
  '</table>';
document.getElementById("resultDiv").innerHTML = htmlTable;
<!DOCTYPE html>
<html>

<head>
</head>

<body>
  <div id="resultDiv"></div>
</body>

</html>
© www.soinside.com 2019 - 2024. All rights reserved.