1

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

 {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;
}

2 Answers2

2

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

Amila Senadheera
  • 12,229
  • 15
  • 27
  • 43
0

If you want to link value and label this way:

ak => 1
pk => 2
plk => 3
k => 5

It would be better practice to change your data structure and move them aside. It avoids running in cases where label[x] is defined, but value[x] is not:

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

  return result.map((el) => {
    return (
      <div>
        <h1>{el.name}</h1>
        <div className="vart">
          <div>
            {el.items.map((e, index) => {
              return (
                <p>
                  {e.label} : {e.value}
                </p>
              );
            })}
          </div>
        </div>
      </div>
    );
  });
}
cadesalaberry
  • 612
  • 1
  • 8
  • 17