2

Here is my Jason data and code that i am using map() for populating my data but getting some error, below is my code

var data = [
  {
    categorytitle: "Shoes",
    category: [
      {
        Category1: "Boots"
      },
      {
        Category2: "Sneakers"
      },
      {
        Category3: "Flats"
      },
      {
        Category4: "Booties"
      },
      {
        Category5: "Mules"
      },
      {
        Category6: "Heels/Pumps"
      },
      {
        Category7: "Clogs"
      },
      {
        Category8: "Slippers"
      },
      {
        Category9: "Sandals"
      },
      {
        Category10: "Sale"
      },
      {
        Category11: "Shop All"
      }
    ]
  }
];

also please find the populating code at below...

{data.map((el, e) => (
                    <div {...el} key={e}>
                      <h3>{el.categorytitle}</h3>
                      <ul>
                        <li>{el.category[e]}</li>
                      </ul>
                    </div>
                  ))}

Please guide me how i can display Category1 .... in list?

2
  • 1
    What is the error that you are getting? Commented Feb 5, 2019 at 20:41
  • @MazharHaque this is error i am getting... Objects are not valid as a React child (found: object with keys {Category1}). If you meant to render a collection of children, use an array instead. Commented Feb 5, 2019 at 20:46

3 Answers 3

1

You can do something like below

   {data.map((ele, index) => (
                <div key={"Key-"+index}>
                  <h3>{ele.categorytitle}</h3>
                  <ul>
                    {Array.isArray(ele.category) && ele.category.map((d, i) => (<li key={"Key-"+i}>{d[`Category${i+1}`]}
                    </li>))}
                  </ul>
                </div>
              ))}
Sign up to request clarification or add additional context in comments.

Comments

0

Try something like this: You need to loop through the category array

data[0].category.map((el, i) => {
    console.log(el['Category' + i++])
    console.log(i)
    return el['Category' + i++];
})

Comments

0

const data = [{
  categorytitle: "Shoes",
  category: [{
      Category1: "Boots"
    },
    {
      Category2: "Sneakers"
    },
    {
      Category3: "Flats"
    },
    {
      Category4: "Booties"
    },
    {
      Category5: "Mules"
    },
    {
      Category6: "Heels/Pumps"
    },
    {
      Category7: "Clogs"
    },
    {
      Category8: "Slippers"
    },
    {
      Category9: "Sandals"
    },
    {
      Category10: "Sale"
    },
    {
      Category11: "Shop All"
    }
  ]
}];

const category = data.forEach(({
  category
}) => {
  Object.values(category).map((value, index) => {
    console.log(value[`Category${index +1 }`]);
  })
});

For React JSX it should be something like :

{data.forEach(({ category }) => {
      Object.values(category).map((value, index) => {
        <span> {value[`Category${index + 1}`]} </span>;
      });
    })}

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.