Javascript 在动态表中显示对象数组javascript
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29335369/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 03:13:20 来源:igfitidea点击:
display array of objects in a dynamic table javascript
提问by V.V
I want to display an array of objects in a dynamic table using javascript.
我想使用 javascript 在动态表中显示一组对象。
var rows=[{ name : "John", age:20, email: "[email protected]"},
{ name : "Hyman", age:50, email: "[email protected]"},
{ name : "Son", age:45, email: "[email protected]"}
........................etc
];
This is how it looks.I want to know how can I show this as a dynamic table.
这是它的外观。我想知道如何将其显示为动态表。
回答by Prashant Ghimire
This is how you do it:
这是你如何做到的:
Javascript Solution:
Javascript 解决方案:
小提琴:
var rows = [{
name: "John",
age: 20,
email: "[email protected]"
}, {
name: "Hyman",
age: 50,
email: "[email protected]"
}, {
name: "Son",
age: 45,
email: "[email protected]"
}];
var html = "<table border='1|1'>";
for (var i = 0; i < rows.length; i++) {
html+="<tr>";
html+="<td>"+rows[i].name+"</td>";
html+="<td>"+rows[i].age+"</td>";
html+="<td>"+rows[i].email+"</td>";
html+="</tr>";
}
html+="</table>";
document.getElementById("box").innerHTML = html;
jQuery Solution:
jQuery 解决方案:
var rows = [{
name: "John",
age: 20,
email: "[email protected]"
}, {
name: "Hyman",
age: 50,
email: "[email protected]"
}, {
name: "Son",
age: 45,
email: "[email protected]"
}];
$(document).ready(function () {
var html = "<table border='1|1'>";
for (var i = 0; i < rows.length; i++) {
html+="<tr>";
html+="<td>"+rows[i].name+"</td>";
html+="<td>"+rows[i].age+"</td>";
html+="<td>"+rows[i].email+"</td>";
html+="</tr>";
}
html+="</table>";
$("div").html(html);
});

