In React 16.11.0, I used map() function for a simple JSON object to access the values of each elements. When ReactDOM renders the content, the table will display a list of values in column for each elements of data:
const data = [
{ Name: "Jame", Email: "[email protected]", ID: "1568132456", City: "New York" },
{ Name: "Harris", Email: "[email protected]", ID: "7666487798", City: "Chicago" },
{ Name: "Daisy", Email: "[email protected]", ID: "2177897432", City: "Los Angeles" },
// etc...
];
const Table = ({data}) => {
const head = (
<tr>
<th>Name</th>
<th>Email</th>
<th>ID</th>
<th>City</th>
</tr>
);
const body = data.map(element => (
<tr key={element.ID}>
<td>{element.Name}</td>
<td>{element.Email}</td>
<td>{element.ID}</td>
<td>{element.City}</td>
</tr>
));
return (
<table>
<thead>{head}</thead>
<tbody>{body}</tbody>
</table>
);
};
ReactDOM.render(<Table data={data}/>, document.querySelector('div'));
<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>
<div></div>
The table output will display these values below:
Instead, what I want is the values will be displayed in row for each elements like this table below:
How to convert the table in this case?

