Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

238
Vistas
Make a new div element for each element in an array with JSX?

I am trying to make a div element for each element in an array using reactjs JSX. I was not sure how to really go about this but I tried doing this:

{results.forEach(element => {
               <div className={classes.SearchResults}>

                {results[element]}    

                </div>

})}

This didn't work but I am fairly confident that it is something along these lines. I receive no errors results is an array element I defined elsewhere and that is working completely. The only issue is displaying a new div element for each of elements within the results array. If you need more code I am happy to give you it though I think this should be a sufficient amount.

about 4 years ago · Juan Pablo Isaza
3 Respuestas
Responde la pregunta

0

Close.

First, you want .map(), not .forEach(). The latter just iterates but doesn't return anything, whereas the former returns a new array (with the same size as the original) "mapped" to a new element structure (in this case JSX elements).


Aside from that... The use of curly braces {} here creates a function body, which in this case consists of a JSX element but doesn't return anything:

{
  <div className={classes.SearchResults}>
    {results[element]}    
  </div>
}

You can return the element explicitly:

{
  return (<div className={classes.SearchResults}>
    {results[element]}    
  </div>);
}

Or use an implicit return from the arrow function by using parentheses () around it to have the whole function be just one statement (instead of an explicit function body):

(
  <div className={classes.SearchResults}>
    {results[element]}    
  </div>
)

Additionally... The use of results[element] looks wrong here. The first callback argument to .map() isn't the index, it's the element itself. I suspect you want this:

(
  <div className={classes.SearchResults}>
    {element}    
  </div>
)

As a final note, in React you'll see warnings if you iterate over an array like this to produce JSX but don't supply a key property to the JSX element. If the element object has an identifier, such as an id property, you can use that:

(
  <div key={element.id} className={classes.SearchResults}>
    {element}    
  </div>
)

But failing that, you can always rely on the second argument to the .map() callback, which is just the array element's index:

{results.map((element, i) => (
  <div key={i} className={classes.SearchResults}>
    {element}    
  </div>
))}
about 4 years ago · Juan Pablo Isaza Denunciar

0

You're close, but you're using the wrong array method here. Array.map() returns a new array, and you can use that to return an array of divs to render for each item in the results array.

For more details, take a look here: https://reactjs.org/docs/lists-and-keys.html

about 4 years ago · Juan Pablo Isaza Denunciar

0

forEach just runs your callback ignoring the return value

map is used to transform and return a new value for each iteration, hence you can use the following instead:

{results.map((element, index) => {
   return (
      <div className={classes.SearchResults} key={index}>
          {results[element]}    
      </div>
   )
})}
about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda