我试图迭代这个JSON,所以我可以创建一个表,但由于有很多标题和许多数据,我怎么能不通过这样做。
const BBDDCustomer = {
ui_labels: {
name: 'Name',
address: 'address',
postalCode: 'Postal Code',
city: 'City',
country: 'Country',
telephone: 'Telephone',
email: 'Email',
modified: 'Modified',
delete: 'Delete'
},
data: [
{
name: 'n1',
address: 'a1',
postalCode: 'PC 1',
city: 'c 1',
country: 'cou 1',
telephone: 'tel 1',
email: 'em 1'
}
}
我不必像这样写:
<table striped bordered condensed hover responsive>
<thead>
<tr>
<th>{BBDDCustomer.ui_labels.name}</th>
<th>{BBDDCustomer.ui_labels.address}</th>
<th>{BBDDCustomer.ui_labels.postalCode}</th>
<th>{BBDDCustomer.ui_labels.city}</th>
<th>{BBDDCustomer.ui_labels.country}</th>
<th>{BBDDCustomer.ui_labels.telephone}</th>
<th>{BBDDCustomer.ui_labels.email}</th>
<th>{BBDDCustomer.ui_labels.modified}</th>
<th>{BBDDCustomer.ui_labels.delete}</th>
</tr>
</table>
答案 0 :(得分:0)
您需要按照您想要的顺序枚举属性
const columns = ['name', 'address', ...];
并使用map
<Table striped bordered condensed hover responsive>
<thead>
<tr>
{
columns.map(column => (
<th key={column}>{BBDDCustomer.ui_labels[column]}</th>
))
}
</tr>
</thead>
<tbody>
{
BBDDCustomer.data.map((data, i) =>(
<tr key={i}>
{
columns.map(column => (
<td key={column + i}>{data[column]}</td>
))
}
</tr>
))
}
</tbody>
</Table>