2 回答

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

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