Skip to content

Instantly share code, notes, and snippets.

@xmagee
Created August 5, 2021 03:38
Show Gist options
  • Select an option

  • Save xmagee/d13a4a8c29ffe259e86f97dfc6a0d06b to your computer and use it in GitHub Desktop.

Select an option

Save xmagee/d13a4a8c29ffe259e86f97dfc6a0d06b to your computer and use it in GitHub Desktop.
Generic table
export default function App() {
const data1 = [
{
"name": "Jane Doe",
"email": "[email protected]",
"phone": "(123) 456-7890"
},
{
"name": "John Smith",
"email": "[email protected]",
"phone": "(234) 567-8901"
},
{
"name": "Jim Example",
"email": "[email protected]",
"phone": "(345) 678-9012"
}
]
return (
<GenericTable tdata={ data1 } />
)
}
function GenericTable(props) {
const { tdata } = props
return (
<table>
<thead>
{ tdata.length > 0 && (
Object.keys(tdata[0]).map((key, keyIndex) => (
<th key={keyIndex}>{key}</th>
))
)}
</thead>
<tbody>
{ tdata.length > 0 && (
tdata.map((row, rowIndex) => (
<tr key={rowIndex}>
{Object.keys(tdata[0]).map((key, keyIndex) => (
<td key={`${rowIndex}_${keyIndex}`}>{row.[key]}</td>
))}
</tr>
))
)}
</tbody>
</table>
)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment