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

368
Vistas
Why am I unable to map this array of objects when it is loaded in useState()?

learning React but I'm trying to map an array of objects in a dropdown box.

I'm getting a "Uncaught TypeError: ids.map is not a function".

Why am I getting this error when I have set the loadedIds I get from the GET request into useState's setIds?

https://jsfiddle.net/4jh9c6dv/53/

Thank you for helping a beginner :')

function Dropdown ()
{
    const [ids, setIds] = React.useState([]);

 React.useEffect(() => {
    request.get(endpointIds).then((response) => {
      setIds(response.data);
      
      const loadedIds = [];
      
      for (const id in response)
      {
        loadedIds.push({
        id: ids,
        });
      }
      
      setIds(loadedIds);
      
    });
  }, []);
  
  
 const idsList = ids.map((id) => 
 (
        <option>id</option>
 ));

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

0

You do not need to use setIds(response.data);. Remove that. response.data is not an array, response.data.ids is. That is why you get the error.

EDIT : Use for of instead of for in.

for of is used to iterate over items of an array/iterable object. for in is used to iterate over index.

about 4 years ago · Juan Pablo Isaza Denunciar

0

response.data should be response.data.id you also forgot curly braces {id}

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