logo
logo

Create Table

Beginner

Create Table

Create a function that takes an array of objects and dynamically creates an HTML table with the data. The table should have a header row with column names and a row for each object in the array with data populated in the corresponding columns.

For example, given the following array:

const data = [
  { name: 'John', age: 28, city: 'New York' },
  { name: 'Jane', age: 32, city: 'San Francisco' },
  { name: 'Bob', age: 41, city: 'Chicago' },
];

The resulting table should look like this:

<table>
  <thead>
    <tr>
      <th>Name</th>
      <th>Age</th>
      <th>City</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>John</td>
      <td>28</td>
      <td>New York</td>
    </tr>
    <tr>
      <td>Jane</td>
      <td>32</td>
      <td>San Francisco</td>
    </tr>
    <tr>
      <td>Bob</td>
      <td>41</td>
      <td>Chicago</td>
    </tr>
  </tbody>
</table>

Console
Submit
Solution
00:00