2 回答

TA貢獻1801條經驗 獲得超16個贊
您可以將所有行存儲在一個數組中,然后在以下位置使用它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>
添加回答
舉報