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

3
  • Hello, have you tried to run you code step by step? Take a look at map function with ReactJs on the web. Also if you can edit your question and make it more comfortable for reading. Thanks! Commented Jan 21, 2022 at 9:34
  • I still dont know what ur asking? Do you get an error? Commented Jan 21, 2022 at 9:34
  • I just want to create a cart like an image assign with question Commented Jan 21, 2022 at 9:37

2 Answers 2

1

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

Sign up to request clarification or add additional context in comments.

3 Comments

@Shanu Singh, check this out !!
@Shanu Singh, I have updated the code sandbox with styles as well
tnx bro I checked it already, and how you did that is totally appreciable
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>
    );
  });
}

1 Comment

tnx bro, but what I want Amila Senadheera updated solution for that

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.