1

I've been trying to map over the product object below coming from state to display divs with their corresponding size in React.

{ 
    name: 'Aphelandra',
    image: './prod-img/aphelandra.jpeg',
    description: 'Lorem ipsum dolor sit amet consectetur, adipisicing elit.',
    size: 
    [
      {
      title: 'sm',
      price: 31,
      countInStock: 111,
      },
      {
      title: 'md',
      price: 56,
      countInStock: 24,
      },
      {
      title: 'lg',
      price: 88,
      countInStock: 13,
      }
    ],
  },
  {

I have been trying unsuccesfully to do this with different variations of this function:

<div>
  {product.size.map(title => (
    <div>{title}</div>
  ))}
</div>

Other properties display fine like this: <p>{product.description}</p>

But I cant find a way to get size. Most common error is cannot read property 'map' of undefined

How can I get through this array?

1 Answer 1

1

Do this:

<div>
  {product&&product.size?product.size.map(item => (
    return (<div>{item.title}</div>)
  )):null}
</div>
Sign up to request clarification or add additional context in comments.

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.