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

Error when using map for Html's select, option

Aren’t the following two codes the same code?

But when using map I got a this error caught TypeError: size.option.map is not a function

 <select id="size">
          <option value="">--pleae choose a size--</option>
          <option value="">{size.option.small}</option>
          <option value="">{size.option.medium}</option>
          <option value="">{size.option.large}</option>
          <option value="">{size.option.xlarge}</option>
          <option value="">{size.option.xxlarge}</option>
        </select>
        <select id="size">
          <option value="">--pleae choose a size--</option>
          {size.option.map((s) => (
            <option value={s}>{s}</option>
          ))}
        </select>

enter image description here

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

enter image description here

my json

>Solution :

size.option is an object, not an array therefore the map method is not callable. To iterate over an object you can use Object.keys/values/entries. For example:

<select id="size">
    <option value="">--pleae choose a size--</option>
    {Object.entries(size.option).map(([key, value]) => (
        <option key={key} value={key}>{value}</option>
    ))}
</select>

Don’t forget you must provide a key prop when rendering arrays.

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