【发布时间】:2021-11-12 13:04:42
【问题描述】:
我有一个包含多个记录的对象,格式如下:
["name": 'The Belvedere',
"units":[
{"unit_num": 1,
"street": '1234 Main',
"monthly_rent": 900,
"lease_expiration" 2021-11-01
},
{"unit_num": n,
"street": 'Some address',
"monthly_rent": 900,
"lease_expiration" 2021-11-01
}
]
对于多个“名称”中的每一个,它们都有一个且只有一个“units”数组,并且在“units”数组中,将有一对多的“unit_num”与关联的“street”。我正在尝试生成一个看起来像这样的平面表。
| name | street | rent | lease |
|---|---|---|---|
| The Belvedere | 1234 Main | 900 | 2021-11-01 |
| The Belvedere | 1235 Main | 875 | 2022-03-21 |
| The Grayson | 345 Maple | 925 | 2023-10-31 |
| ... |
目前的代码是这样的: 屏幕映射“租约”对象并调用“租约”组件并传递单个“租约”条目。
return (
<div className="container">
<h1>Leases</h1>
<div className="row justify-content-center mt-5 lease">
{loading ? (
<h1>
<Loader />
</h1>
) : error ? (
<h1>Error</h1>
) : (
leases.map((lease) => {
return (
<div className="col-md-9">
<Lease lease={lease} />
</div>
);
})
)}
</div>
</div>
);
Lease 组件将其打印到屏幕上。
return (
<div>
<MDBTable hover>
<MDBTableBody>
<tr>
<td>{name}</td>
<td>{street}</td>
<td>{monthly_rent}</td>
<td>{lease_expiration}</td>
</tr>
</MDBTableBody>
</MDBTable>
</div>
);
我面临的挑战是因为我正在迭代对象,我可以让每一行显示,但它们不在表格中,只是屏幕上的单独行。
但我不知道如何将整个数据集对象传递给 Lease 组件,并在该组件中迭代然后填充并返回填充的表,而不是单独传递“名称”。
有什么建议吗?
【问题讨论】:
标签: javascript reactjs