Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

create card using array of data react

hi everyone I have data given below by using this data I just want to create a cart click on this link to check what kind of cart I want to design from this data

 const result = [
{
  name: 'shanu',
  label: ['ak', 'pk', 'plk', 'k'],
  value: [1, 2, 3, 5],
},

];

// what i did

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

 {result.map((el) => {
        return (
          <div>
            <h1>{el.name}</h1>
            <div className="vart">
              <div>
                {el.label.map((e) => {
                  return <p>{e}</p>;
                })}
              </div>
              <div>
                {el.value.map((e) => {
                  return <p>{e}</p>;
                })}
              </div>
            </div>
          </div>
        );
      })}
.vart {
  display: flex;
  flex-direction: row;
}

>Solution :

You can access the value according to the index of the label as below. You can use a CSS grid system to show a two-column view.

export default function App() {
  const result = [
    {
      name: "shanu",
      label: ["ak", "pk", "plk", "k"],
      value: [1, 2, 3, 5]
    }
  ];

  return result.map((el) => {
    return (
      <div>
        <div className="header">{el.name}</div>
        <div className="grid-container">
          {el.label.map((e, index) => {
            return (
              <div
                className="grid-item"
                style={{ textAlign: index % 2 === 0 ? "left" : "right" }}
              >
                {e} : {el.value[index]}
              </div>
            );
          })}
        </div>
      </div>
    );
  });
}

Following styles will organise the grid with right aligning the second column.

.header {
  color: #ffffff;
  background-color: #4473c4;
  padding: 10px 20px;
}

.grid-container {
  display: grid;
  grid-template-columns: auto auto;
}

.grid-item {
  padding: 10px 20px;
  color: #ffffff;
  background-color: #91cf50;
}

HTML Output

enter image description here

Code Sandbox

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading