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

How to loop over array of objects?

I want to loop over this array of objects to render them in a component in react, why the mapping is not working?

fruits:[{"value":"Apple","status":"Green"},{"value":"Orange ","status":"Yellow"},{"value":"Banana","status":"Green"}]

export interface FruitsStatus { 
  fruits: Record<string, string>;
}
const Fruits: (props: FruitsStatus) => JSX.Element = props => {
return (
{props.fruits.map((fruit, index) => {
              return (
                <tr key={index}>
                  <td>
                    {fruit.value}
                  </td>
                  <td > {fruit.status} </td>
                </tr>
              );
            })}
)
}

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

>Solution :

I think this is because there is no root tag to contain all tr tags

simply wrap it with fragment :

return (
   <>
     {props.fruits.map((fruit, index) => {
              return (
                <tr key={index}>
                  <td>
                    {fruit.value}
                  </td>
                  <td > {fruit.status} </td>
                </tr>
              );
            })}
   </>
)
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