2 回答
TA贡献1801条经验 获得超15个赞
您可以将所有行存储在一个数组中,然后在以下位置使用它table:
export default function App() {
const arr1 = ["item1","item2","item3","item4"]
const arr2 = ["price1","price2","price3","price4"]
const rows = []
for (const [index, value] of arr1.entries()) {
rows.push(
<tr key={index}>
<td>{value}</td>
<td>{arr2[index]}</td>
</tr>
)
}
return (
<div className="App">
<table>
<tbody>
{rows}
</tbody>
</table>
</div>
);
}
TA贡献1752条经验 获得超4个赞
如果数组总是有相同的长度,你可以使用 map 或类似的东西。
<table>
{
arr1.map((element, index) => <tr>
// The first one is the nth element from the array
// The second one we just access through index
<td>{element}</td>
<td>{arr2[index]}</td>
</tr>
)
}
</table>
或者
<table>
{
Array(arr1.length).map((element, index) => <tr>
// We just access through index
<td>{arr1[index]}</td>
<td>{arr2[index]}</td>
</tr>)
}
</table>
添加回答
举报