Home > Software engineering >  If clause in JSX map function
If clause in JSX map function

Time:11-30

So i have this code to generate multiple dropdown lists.

<div>
  {['Mischung', 'Menge'].map((key) => (
    <select key={key}>
      {mischung.map(({ [key]: value }) => (
        <option key={value}>{value}</option>
      ))}
    </select>
  ))}
</div>

Every dropdown list is beeing generated, but with empty values.

enter image description here

enter image description here

As you can see there are empty values from the other dropdown.

How can i check the value for null or if its not empty.

CodePudding user response:

<div>
  {['Mischung', 'Menge'].map((key) => (
    <select key={key}>
      {mischung.map(({ [key]: value }) => {
        if (value) {
          return (
            <option key={value}>{value}</option>
          )}
        }
      )}
    </select>
  ))}
</div>
  • Related