Skip to content

Instantly share code, notes, and snippets.

@sanusart
Created July 18, 2020 14:31
Show Gist options
  • Save sanusart/e0e37444a8d03862e4294961cd491e88 to your computer and use it in GitHub Desktop.
Save sanusart/e0e37444a8d03862e4294961cd491e88 to your computer and use it in GitHub Desktop.
React [{}] to table #reactjs
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