Search code examples
javascriptreactjshtml-tablewidgetkeyvaluepair

How can i display key value pairs in a table grid in react js.?


I'm trying to display key-value pairs in react js,

but somehow I cannot display them properly. I have created a table widget and I am not getting the right display

My table Widget

import React from "react";

function Table(props) {
  const { tablevalue } = props;

  console.log(tablevalue);

  return (
    <div className="table">
      <table className="table table-hover">
        <tbody>
          {tablevalue.map((item, value) =>
            Object.entries(item, (key, value) => {
              return (
                <tr className="table-row">
                  <th scope="col" key={`tablevalue-${key}`}>
                    {key}
                  </th>
                  <td key={`tablevalue-${value}`}>{value}</td>
                </tr>
              );
            })
          )}
        </tbody>
      </table>
    </div>
  );
}

export default Table;

app.js

import React, { Fragment } from "react";

import Dropdown from './components/widgets/Dropdown/index'
import Table from './components/widgets/Table/index'

function DropdownTest() {
  return (
      <h3>
        <b>Profit</b>
      </h3>
      <br />
        <Table 
        tablevalue = {[{key:'Binance' , value: 'Polonix'}, {key:'Profit' , value:'Loss'}]}
        />
      </div>

  );
}
export default DropdownTest;

My output

Whereas I want my output to be displayed in terms of table


Solution

  • You can use table header

      <tbody>
        <tr>
          <th>Key</th>
          <th>Value</th>
        </tr>
        {tablevalue.map(({ key, value }) => (
          <tr className="table-row">
            <td key={`tablevalue-${key}`}>{key}</td>
            <td key={`tablevalue-${value}`}>{value}</td>
          </tr>
        ))}
      </tbody>
    

    Code

    const Table = ({ tablevalue }) => (
      <div className="table">
        <table className="table table-hover">
          <tbody>
            <tr>
              <th>Key</th>
              <th>Value</th>
            </tr>
            {tablevalue.map(({ key, value }) => (
              <tr className="table-row">
                <td key={`tablevalue-${key}`}>{key}</td>
                <td key={`tablevalue-${value}`}>{value}</td>
              </tr>
            ))}
          </tbody>
        </table>
      </div>
    );
    
    const DropdownTest = () => (
      <div>
        <h3>
          <b>Profit</b>
        </h3>
        <br />
        <Table
          tablevalue={[
            { key: "Binance", value: "Polonix" },
            { key: "Profit", value: "Loss" }
          ]}
        />
      </div>
    );
    
    ReactDOM.render(
      <React.StrictMode>
        <DropdownTest />
      </React.StrictMode>,
      document.getElementById("root")
    );
    <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 id="root"></div>