1

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

keikai
  • 14,085
  • 9
  • 49
  • 68
Abdul Rauf
  • 90
  • 2
  • 14

5 Answers5

1

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>
Jay
  • 2,826
  • 2
  • 13
  • 28
0

You can do it this way:

import React from "react";

export default props => {
  console.log(props.tablevalue);
  const ths = props.tablevalue.map(({ key, value }) => (
    <th key={value}>{key}</th>
  ));
  const values = props.tablevalue.map(obj => <td>{obj.value}</td>);

  return (
    <>
      <table>
        <tr>{ths}</tr>
        <tr>{values}</tr>
      </table>
    </>
  );
};

Here's a stackblitz that displays the table.

Ultimately it all comes down to how you want that table to be displayed, you can tweak some things as you want.

Ramesh Reddy
  • 10,159
  • 3
  • 17
  • 32
0

I've not seen Object.entries used this way. I would usually use a forEach on it:

Object.entries(item).forEach(entry => {
  let key = entry[0];
  let value = entry[1];
});

Though a little bit more performant would be using a for in instead, as Object.entries creates a brand new array then forEach iterates through that, which is unnecessary:

for (const key in item) {
  let value = obj[key];
}
Shaun E. Tobias
  • 527
  • 3
  • 13
0

Check this sandbox: https://codesandbox.io/s/goofy-breeze-fdcdr

Object.keys(tablevalue[0]).map((key) => {key}). You can use this in header tr and loop over it then

Change the Table component to something like this:

import React from "react";

function Table(props) {
  const { tablevalue } = props;
  return (
    <div className="table">
      <table className="table table-hover">
        <tbody>
          <tr>
            {Object.keys(tablevalue[0]).map(key => (
              <th key={key}>{key}</th>
            ))}
          </tr>
          {tablevalue.map(({ key, value }) => (
            <tr className="table-row" key={`tablevalue-${key}`}>
              <td>{key}</td>
              <td>{value}</td>
            </tr>
          ))}
        </tbody>
      </table>
    </div>
  );
}

export default Table;

joy08
  • 9,004
  • 8
  • 38
  • 73
0

I think you have to create a header separately. and after that loop thought the data and apply css for table row. please find the below code.

    import React from 'react';
import './index.css';
function Table(props) {

    const {
        tablevalue,
    } = props

    console.log(tablevalue)

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

export default Table;