3 回答

TA貢獻(xiàn)1946條經(jīng)驗(yàn) 獲得超4個(gè)贊
在您的 JSON 對(duì)象中,您使用的是二維數(shù)組。我將其替換為一維數(shù)組,因?yàn)闆](méi)有必要使用二維數(shù)組。您在某些時(shí)候以錯(cuò)誤的方式使用了索引,我已經(jīng)修復(fù)了。
const chartJSON = JSON.parse(`{
"title": ["KM PER HOUR", "MILES PER HOUR", "MINS PER KM", "MINS PER MILE", "5K", "10K", "HALF MARATH0N", "MARATHON"]
,
"paceChart": [
["7.00kph", "4.35mph", "8:34", "13:40", "00:42:51", "01:25:42", "03:00:51", "06:01:42"],
["7.10kph", "4.41mph", "8:27", "13:36", "00:42:15", "01:24:30", "02:58:18", "05:56:37"],
["7.20kph", "4.47mph", "8:20", "13:25", "00:41:40", "01:23:20", "02:55:50", "05:51:40"],
["7.30kph", "4.54mph", "8:13", "13:14", "00:41:05", "01:22:11", "02:53:25", "05:46:50"]
]
}`);
const table =
<table>
<tbody>
<tr>
{chartJSON.title.map((data, i) => <th key={i}>{data}</th> )}
</tr>
{
chartJSON.paceChart.map((rowData,rowIndex)=> {
return <tr key={rowIndex}>
{
rowData.map(cellData=> <td> {cellData} </td>)
}
</tr>
})
}
</tbody>
</table>
const root = document.querySelector('body');
ReactDOM.render(table,root);
td,th,table{
text-align:center;
border:1px solid dodgerblue;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.6.3/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.6.3/umd/react-dom.production.min.js"></script>

TA貢獻(xiàn)1869條經(jīng)驗(yàn) 獲得超4個(gè)贊
這是更新的答案。我刪除了身體中的重復(fù)地圖。
<table>
<tbody>
<tr>
{chartJSON.title[0].map((data, i) => {
return <th key={i}>{data}</th>;
})}
</tr>
{chartJSON.paceChart.map((data, index) => {
return (
<tr>
{data.map((data2, i) => {
return <td key={i}>{data2}</td>;
})}
</tr>
);
})}
</tbody>
</table>;

TA貢獻(xiàn)1828條經(jīng)驗(yàn) 獲得超6個(gè)贊
嘗試這個(gè)
render() {
return (
<table>
<tbody>
<tr>
{this.state.chartJSON.title.map((data, i) => <th key={"h${i}"}>{data}</th>)}
</tr>
{
this.state.chartJSON.paceChart.map((row, index)=>
<tr>{row.map((item,i)=> <td key={i}>{item}</td>)}</tr>
)}
</tbody>
</table>
)
}
添加回答
舉報(bào)