Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Converting array data into a table in React

I have an array with three objects in it. I want to map all of them and render them in a table in React. I am able to extract values using the map method, but I am not sure how to render them as a table.

[
   {
      "email":"[email protected]",
      "firstname":"gowtham",
      "lastname":"ss",
      "password":"outlook010"
   },
   {
      "email":"[email protected]",
      "firstname":"ss",
      "lastname":"ss",
      "password":"ss"
   },
   {
      "email":"[email protected]",
      "firstname":"gow",
      "lastname":"gow",
      "password":"gow"
   }
]

image from chrome developer tools

Following is the code I used to map the array data:

const exportHeaderData = Object.values(this.state.registeredData).map(
            (data) => {
                return Object.entries(data).map((key,value) => {
                    return `${key}: ${value}`;
                });
            }
        );

I want to render it using a table in react.

like image 651
Gowthamss Avatar asked Jun 13 '26 00:06

Gowthamss


1 Answers

You can do it like this:

<table>
  <thead>
    <tr>
      <th>First name</th>
      <th>Last name</th>
      <th>Password</th>
      <th>Email</th>
    </tr>
  </thead>
  <tbody>
    {arrayWithData.map(item => {
      return (
        <tr key={item.password}>
          <td>{ item.firstname }</td>
          <td>{ item.lastname }</td>
          <td>{ item.password }</td>
          <td>{ item.email }</td>
        </tr>
      );
    })}
  </tbody>
</table>
like image 191
aopanasenko Avatar answered Jun 14 '26 17:06

aopanasenko



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!