2 回答

TA貢獻1836條經驗 獲得超3個贊
快速簡單的解決方案: 不推薦
如果你總是假設dates和cases總是相同的長度,那么你可以這樣做:
{this.state.dates.map((el, index) => (
<tr>
<td>{this.state.dates[index]}</td>
<td>{this.state.cases[index]}</td>
</tr>
))}
該方法利用了函數index的參數map。然后您可以訪問指定索引處的數組。
推薦解決方案:
通常的做法是按記錄對數據進行分組。
使用您的示例,使用如下結構:
state = {
records: [
{ date: '2000', caseId: '1' },
{ date: '2001', caseId: '2' },
{ date: '2002', caseId: '3' }
],
}
然后像這樣實現它:
{this.state.records.map(({ date, caseId }) => (
<tr>
<td>{date}</td>
<td>{caseId}</td>
</tr>
))}
我使用caseIdnot 來代替,case因為它case是 JavaScript 中語句的保留字switch。

TA貢獻1864條經驗 獲得超6個贊
您可以使用index映射函數中的參數并訪問案例數組
<tbody>
{this.state.dates.map((el, index) => (
<tr>
<td>{el}</td>
<td>{this.state.cases[index]}</td>
</tr>
))}{" "}
</tbody>;
- 2 回答
- 0 關注
- 170 瀏覽
添加回答
舉報