请用js把数据在页面中以一个表格显示出来 var arr = [ {name: '小明', age: 12, sex: '男'}, {name: '小红', age: 32, sex: '女'}, {name: '小天', age: 45, sex: '女'}, {name: '小哈', age: 16, sex: '男'}, ]
时间: 2023-04-02 21:01:04 浏览: 82
把数据以表格显示的代码如下:
```html
<table>
<thead>
<tr>
<th>姓名</th>
<th>年龄</th>
<th>性别</th>
</tr>
</thead>
<tbody>
<script>
var arr = [
{name: '小明', age: 12, sex: '男'},
{name: '小红', age: 32, sex: '女'},
{name: '小天', age: 45, sex: '女'},
{name: '小哈', age: 16, sex: '男'},
];
for (var i = ; i < arr.length; i++) {
document.write('<tr>');
document.write('<td>' + arr[i].name + '</td>');
document.write('<td>' + arr[i].age + '</td>');
document.write('<td>' + arr[i].sex + '</td>');
document.write('</tr>');
}
</script>
</tbody>
</table>
```
相关问题
请把数据在页面中以一个表格显示出来 var arr = [ {name: '小明', age: 12, sex: '男'}, {name: '小红', age: 32, sex: '女'}, {name: '小天', age: 45, sex: '女'}, {name: '小哈', age: 16, sex: '男'}, ]
| 姓名 | 年龄 | 性别 |
| ---- | ---- | ---- |
| 小明 | 12 | 男 |
| 小红 | 32 | 女 |
| 小天 | 45 | 女 |
| 小哈 | 16 | 男 |
有如下数组;请筛选出数组中年龄小年18的对象 var arr = [ {name: '小明', age: 12}, {name: '小红', age: 32}, {name: '小天', age: 45}, {name: '小哈', age: 16}, ]
可以使用 filter 方法来筛选出年龄小于18的对象:
var arr = [
{name: '小明', age: 12},
{name: '小红', age: 32},
{name: '小天', age: 45},
{name: '小哈', age: 16},
];
var result = arr.filter(function(obj) {
return obj.age < 18;
});
console.log(result); // [{name: '小明', age: 12}, {name: '小哈', age: 16}]
阅读全文