Created
August 5, 2021 03:38
-
-
Save xmagee/d13a4a8c29ffe259e86f97dfc6a0d06b to your computer and use it in GitHub Desktop.
Generic table
This file contains hidden or 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
| 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