有一个学员成绩json数组如下,要求以表格方式显示学员成绩表。 let scores = [ {"sid":1,"name":"岳不群","chinese":66,"maths":78}, {"sid":1,"name":"风清扬","chinese":86,"maths":65}, {"sid":1,"name":"东方不败","chinese":95,"maths":88}, {"sid":1,"name":"布袋和尚","chinese":36,"maths":69}, {"sid":1,
时间: 2023-06-28 10:06:48 浏览: 42
首先,我们可以将这个 JSON 数组转换成 HTML 表格。可以使用如下代码:
```javascript
let scores = [
{"sid":1,"name":"岳不群","chinese":66,"maths":78},
{"sid":2,"name":"风清扬","chinese":86,"maths":65},
{"sid":3,"name":"东方不败","chinese":95,"maths":88},
{"sid":4,"name":"布袋和尚","chinese":36,"maths":69},
{"sid":5,"name":"林平之","chinese":99,"maths":100}
];
let table = "<table><tr><th>学号</th><th>姓名</th><th>语文</th><th>数学</th></tr>";
for(let i=0; i<scores.length; i++) {
table += "<tr>";
table += "<td>" + scores[i].sid + "</td>";
table += "<td>" + scores[i].name + "</td>";
table += "<td>" + scores[i].chinese + "</td>";
table += "<td>" + scores[i].maths + "</td>";
table += "</tr>";
}
table += "</table>";
document.write(table);
```
上面的代码会生成一个包含学员成绩的 HTML 表格,并将其输出到页面中。输出的表格如下所示:
| 学号 | 姓名 | 语文 | 数学 |
| --- | --- | --- | --- |
| 1 | 岳不群 | 66 | 78 |
| 2 | 风清扬 | 86 | 65 |
| 3 | 东方不败 | 95 | 88 |
| 4 | 布袋和尚 | 36 | 69 |
| 5 | 林平之 | 99 | 100 |
如果需要在页面中显示这个表格,可以将上面的代码放到一个 HTML 文件中,并在浏览器中打开该文件。