0

I'm looking for a way to display a table like the following image in my react app table https://www.depicus.com/swim-bike-run/pace-conversion-chart

And here is the raw data I transferred to JSON. raw data

I was struggling with how to display all the data like the first image in my app.

Here is the code I did try.

        <table>
            <tbody>
                <tr>
                    {chartJSON.title.map((data, i) => <th key={i}>{data[i]}</th>)}
                </tr>
                
                {chartJSON.paceChart.map((data, index) => {
                    return <tr key={index}>{data[0]}</tr>
                })}
            
                {chartJSON.paceChart.map((data, index) => {
                    return <tr key={index}>{data[1]}</tr>
                })}
            </tbody>
        </table>

Here is some of the raw data

{
"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"]
   ]
}

Can anyone help me? Thanks!

2
  • Don't use images to show text data. You can write JSON data as text in your post. Commented Sep 15, 2020 at 7:42
  • Sorry about that, I just added some of the raw data I created Commented Sep 15, 2020 at 7:51

3 Answers 3

1

In your JSON object, You are using a 2D array. I replaced it with 1D array as it's not necessary to use 2D array. You were using indexes in wrong way at some points I have fixed that.

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>

Sign up to request clarification or add additional context in comments.

Comments

0

try this

 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>
    )
  }

fiddler

Comments

0

This is updated answer. I remove duplicate map in body.

    <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>;

1 Comment

It closes to my answer. But all the words were messed up. Any way to fix this? imgur.com/a/8y5cKEL

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.