gpt4 book ai didi

javascript - 如何使用 JSON 数据创建 html 表格?

转载 作者:行者123 更新时间:2023-11-30 11:41:14 25 4
gpt4 key购买 nike

总而言之,我想使用 JSON 数据制作表格格式。我有这样的 JSON 数据

var jsondata = [
{
id: 1,
name: 'Mani',
subject: English,
Score: 88
},
{
id: 1,
name: 'Mani',
subject: Maths,
Score: 65
},
{
id: 2,
name: 'Ram',
subject: English,
Score: 75
},
{
id: 3,
name: 'Kumar',
subject: English,
Score: 15
},
{
id: 3,
name: 'Kumar',
subject: science,
Score: 88
}];

在此数据中,我想像这样更改格式。我无法附上屏幕截图,因此请检查下面的输出代码。与输出相同,只是我需要使用 javascript

<table border='1'>
<tr>
<th>
ID
</th>
<th>
Name
</th>
<th>
English
</th>
<th>
Maths
</th>
<th>
science
</th>
</tr>
<tr>
<td>
1
</td>
<td>
Mani
</td>
<td>
88
</td>
<td>
65
</td>
<td>
-
</td>
</tr>
<tr>
<td>
2
</td>
<td>
Ram
</td>
<td>
75
</td>
<td>
-
</td>
<td>
-
</td>
</tr>
<tr>
<td>
3
</td>
<td>
Kumar
</td>
<td>
18
</td>
<td>
-
</td>
<td>
88
</td>
</tr>

</table>

var jsondata = [
{
id: 1,
name: 'Mani',
subject: 'English',
Score: 88
},
{
id: 1,
name: 'Mani',
subject: 'Maths',
Score: 65
},
{
id: 2,
name: 'Ram',
subject: 'English',
Score: 75
},
{
id: 3,
name: 'Kumar',
subject: 'English',
Score: 15
},
{
id: 3,
name: 'Kumar',
subject: 'science',
Score: 88
}
]


var obj = jsondata;





var category = [];
var callid = [];
//alert("hai");
for (var i = 0; i < Object.keys(obj).length; i++) {

callid.push(obj[i]['subject']);
category.push(obj[i]['subject']);
}

var uniquecategory = uniquerResult(category);
var uniquecallid = uniquerResult(callid);

console.log(uniquecallid);




function uniquerResult(obj) {
var unique = [];
$.each(obj, function (i,ei) {
if ($.inArray(ei, unique) === -1) unique.push(ei);
})
return unique;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>

最佳答案

var jsondata=[{id:1,name:"Mani",subject:"English",Score:88},{id:1,name:"Mani",subject:"Maths",Score:65},{id:2,name:"Ram",subject:"English",Score:75},{id:3,name:"Kumar",subject:"English",Score:15},{id:3,name:"Kumar",subject:"Science",Score:88}];

var headers = ["Id", "Name", ...jsondata.map(d => d.subject).filter((item,pos,self) => self.indexOf(item) == pos)];

var data = Object.values(jsondata.reduce(function(map, item){
map[item.id] = map[item.id] || {};
map[item.id].id = item.id;
map[item.id].name = item.name;
map[item.id][item.subject] = item.Score;
return map;
}, {}));

function buildHtmlTable(selector) {
var columns = addAllColumnHeaders(headers, selector);
for (var i = 0; i < data.length; i++) {
var d = data[i];
var row$ = $('<tr/>');
row$.append($('<td/>').html(d.id));
row$.append($('<td/>').html(d.name));
row$.append($('<td/>').html(d.English || ""));
row$.append($('<td/>').html(d.Maths || ""));
row$.append($('<td/>').html(d.Science || ""));
$(selector).append(row$);
}
}

function addAllColumnHeaders(myList, selector) {
var headerTr$ = $('<tr/>');

for (var i = 0; i < myList.length; i++) {
var header = myList[i];
headerTr$.append($('<th/>').html(header));
}
$(selector).append(headerTr$);
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<body onLoad="buildHtmlTable('#dataTable')">
<table id="dataTable" border="1">
</table>
</body>

关于javascript - 如何使用 JSON 数据创建 html 表格?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42578551/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com