Created
July 18, 2020 14:31
-
-
Save sanusart/e0e37444a8d03862e4294961cd491e88 to your computer and use it in GitHub Desktop.
React [{}] to table #reactjs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const Table = ({data }) => { | |
if(data.length === 0) return null; | |
const rows = Object.keys(data[1]); | |
return ( | |
<table> | |
<thead> | |
<tr> | |
{rows.map(key => ( | |
<th>{key}</th> | |
))} | |
</tr> | |
</thead> | |
<tbody> | |
{data.map((tr ) => { | |
return ( | |
<tr key={tr.key}> | |
{rows.map((d, i)=> ( | |
<td key={i}>{tr[d] === undefined? 'n/a' : tr[d].toString()}</td> | |
))} | |
</tr> | |
); | |
})} | |
</tbody> | |
</table> | |
); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment