[英]React map over nested array
我从 firebase 获取一组行,每一行看起来像下面的行。
row = [
{
index: 1,
body: 'description'
options: ['option1', 'option2', 'option3']
}
]
我目前正在将这些行渲染到 React 中的表中,如下所示:
{this.state.rows.map((row) => (
<TableRow key={row.visit}>
<TableCell align="left">{row.index}</TableCell>
<TableCell align="left">{row.body}</TableCell>
<TableCell align="left">{row.options}</TableCell>
</TableRow>))}
但是,我试图让选项位于下拉列表或类似列表中,以便看起来更整洁并且不会占用太多垂直空间。 有没有办法通过嵌套数组将 map 和 output 放入下拉列表?
您还可以 map 嵌套数据。 我建议使用select
元素。
{this.state.rows.map((row) => (
<TableRow key={row.visit}>
<TableCell align="left">{row.index}</TableCell>
<TableCell align="left">{row.body}</TableCell>
<TableCell align="left">
<select>
{row.options.map((option, i) => (
<option key={i} value={option}>{option}</option>
))}
</select>
</TableCell>
</TableRow>
))}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.