0

I have an object data which contains array of data. Here is the structure of data object.

EMC: (4) ['EMC Symmetric VMAX', 'EMC Symmetric VMAX', 'EMC Symmetric VMAX', 'EMC Symmetric VMAX']
MICROSOFT: (8) ['OFFICE 365', 'Windows', 'OFFICE 365', 'Windows', 'OFFICE 365', 'Windows', 'OFFICE 365', 'Windows']
Open Source: (4) ['OpenSSL', 'OpenSSL', 'OpenSSL', 'OpenSSL']
Oracle: (12) ['IAM', 'MySQL Server', 'MySQL Server', 'IAM', 'MySQL Server', 'MySQL Server', 'IAM', 'MySQL Server', 'MySQL Server', 'IAM', 'MySQL Server', 'MySQL Server']
RED HAT: (4) ['RHEL', 'RHEL', 'RHEL', 'RHEL']

I managed to display the values of each array as follows(filters unique values of each array)

const DisplayData = Object.values(data)?.map((vendor) => {
    const uniqValues = [...new Set(vendor)];
    return (
      <tr>
        <td>{}</td>
        <td>{uniqValues}</td>
      </tr>
    );
  });
  return (
    <div className="z-100 flex justify-center mb-24 bg-white items-center mt-10">
      <div className="text-black">
        <div className="rounded overflow-hidden flex  justify-center items-center">
          <table class="table table-striped ">
            <thead>
              <tr>
                <th>Vendor</th>
                <th>Product</th>
              </tr>
            </thead>
            <tbody>{DisplayData}</tbody>
          </table>
        </div>
      </div>
    </div>
  );

Here is what the above code displays

array data with table

Finally this is the expected table forma to display the data.

expected result

How can I do this? Thanks

2 Answers 2

1

Ready https://codesandbox.io/s/silent-tree-u4qelb?file=/src/styles.css

enter image description here

Css

table {
  border: 1px solid #d8d8d8;
  border-radius: 4px;
}

table * {
  box-sizing: border-box;
}

th {
  border: 1px solid #d8d8d8;
  padding-top: 14px;
  padding-bottom: 14px;
  min-width: 220px;
  text-align: left;
  padding-left: 10px;
}

td {
  border-top: 1px solid #d8d8d8;
  padding: 8px 0;
  padding-left: 10px;
}

React

function removeDuplicates(data, i) {
  const arr = Object.values(data)[i];
  return arr ? [...new Set(arr)] : [];
}

const Rows = ({ data }) => {
  const keys = Object.keys(data);

  return (
    <>
      {keys.map((key, i) => (
        <tr>
          <td>{key}</td>
          <td>
            {removeDuplicates(data, i).map((item, i) => (
              <div key={i}>{item}</div>
            ))}
          </td>
        </tr>
      ))}
    </>
  );
};

const DisplayData = ({ data }) => {
  const hasData = Object.values(data)?.length;
  return (
    <table>
      <thead>
        <tr>
          <th>Vendor</th>
          <th>Product</th>
        </tr>
      </thead>
      <tbody>{hasData && <Rows data={data} />}</tbody>
    </table>
  );
};

// how you render <DisplayData data={data} />

Data you provided

const data = {
  EMC: [
    "EMC Symmetric VMAX",
    "EMC Symmetric VMAX",
    "EMC Symmetric VMAX",
    "EMC Symmetric VMAX"
  ],
  MICROSOFT: [
    "OFFICE 365",
    "Windows",
    "OFFICE 365",
    "Windows",
    "OFFICE 365",
    "Windows",
    "OFFICE 365",
    "Windows"
  ],
  "Open Source": ["OpenSSL", "OpenSSL", "OpenSSL", "OpenSSL"],
  Oracle: [
    "IAM",
    "MySQL Server",
    "MySQL Server",
    "IAM",
    "MySQL Server",
    "MySQL Server",
    "IAM",
    "MySQL Server",
    "MySQL Server",
    "IAM",
    "MySQL Server",
    "MySQL Server"
  ],
  "RED HAT": ["RHEL", "RHEL", "RHEL", "RHEL"]
};
Sign up to request clarification or add additional context in comments.

2 Comments

I want the product name to be displayed in a single row as i show up in an image above?
@TeshieEthiopia They are bro. Just play around with css. Table format is as you expected. Data is not same. You provided 4 columns of data but the picture you want consist of 2 columns. Also check the html in the browser console. You will see the Products are in the same row.
0

Seems like you need the keys of data as well as the values. You can get that by using object.entries instead of values.

Object.entries(data)?.map(([key, vendor]) => {
    const uniqValues = [...new Set(vendor)];
    return (
      <tr>
        <td>{key}</td>
        <td>{uniqValues}</td>
      </tr>
    );
})

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.