0

I want to create an option for every single profile that I have. I checked and I do have a profile so the profiles array is not empty and profile.profilename isn't undefined. It just doesn't render.

What i see: Picture

<div className="col-4">
   <select name="profile" className="billingSelector">
         <option>Billing profile</option>
             {(getProfiles() as any).forEach((profile:Profile) => {
                  <option>{profile.profileName}</option>
              })}
   </select>
</div>
5
  • 1
    What does getProfiles() return? Why as any? Commented Apr 28, 2021 at 20:45
  • as any is irrelevant, i was just trying to fix it. getProfiles() return a Profile array so i am getting all of its elements. i removed as any from the code Commented Apr 28, 2021 at 20:47
  • Is it by any chance async? What does console.log(getProfiles()) print? Commented Apr 28, 2021 at 20:48
  • console.log(getProfiles()) prints out the array. It has one element which is the profile that I want to show on screen. Commented Apr 28, 2021 at 20:51
  • [p] 0: p {profileName: "test profile #1", firstName: "John", lastName: "Doe", postalCode: "29223", city: "Celle", …} length: 1 __proto__: Array(0) Commented Apr 28, 2021 at 20:51

3 Answers 3

1

Use map instead of foreach.

There is a difference. Map returns list, foreach - not.

More info on that:

Array.prototype.forEach() - JavaScript | MDN

Array.prototype.map() - JavaScript | MDN

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

Comments

1

I found a solution. I used map instead of foreach and i removed curly braces and used regular ones.

<select name="profile" className="billingSelector">
   <option>Billing profile</option>
   {getProfiles().map((profile:Profile)=> (
        <option key={profile.profileName}>{profile.profileName}</option>
   ))}       
</select>

Comments

0

Instead of foreach can you please try map ? E.g

getProfiles().map((profile, index) => ( {profile.profileName}));

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.