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

226
Vistas
How to map an array of objects in react?

I have an array of currencies that I'd like to map out. But I'm not sure how? My app crashes with the code I wrote and returns an empty page with an error: currencyList.map is not a function

This is what I get when I console.log the fetched data:

AED: {currencyName: 'UAE Dirham', id: 'AED'}
AFN: {currencyName: 'Afghan Afghani', currencySymbol: '؋', id: 'AFN'}
ALL: {currencyName: 'Albanian Lek', currencySymbol: 'Lek', id: 'ALL'}
AMD: {currencyName: 'Armenian Dram', id: 'AMD'}
ANG: {currencyName: 'Netherlands Antillean Gulden', currencySymbol: 'ƒ', id: 'ANG'}
AOA: {currencyName: 'Angolan Kwanza', id: 'AOA'}

There are lots more entries there..

anyways this is my code

const [currencyList, setCurrencyList] = useState<any>();

  useEffect(() => {
    axios.get(listOfCurrencies)
    .then(res => {
      setCurrencyList(res.data['results']);
      console.log(res.data['results'])
    })
    .catch(error => {
      console.log("there was an error with connecting to database")
    })

  }, []);

return (
      <div>
        <h1> data will be here </h1>
          {currencyList && currencyList.map(curr => {
            return(
              <div>
                {curr}
              </div>
            )

          })}
      </div>
)
about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

For what I understand, you have an object, not an array as response, kind of:

const currencyList = {
  AED: {currencyName: 'UAE Dirham', id: 'AED'}
  AFN: {currencyName: 'Afghan Afghani', currencySymbol: '؋', id: 'AFN'}
  ALL: {currencyName: 'Albanian Lek', currencySymbol: 'Lek', id: 'ALL'}
  AMD: {currencyName: 'Armenian Dram', id: 'AMD'}
  ANG: {currencyName: 'Netherlands Antillean Gulden', currencySymbol: 'ƒ', id: 'ANG'}
  AOA: {currencyName: 'Angolan Kwanza', id: 'AOA'}
}

So there's no map in objects. You'll need to get the keys, for example:

Object.keys(currencyList).map(
 key => <div>{currencyList[key].id}</div>
)

about 4 years ago · Juan Pablo Isaza Denunciar

0

If you want to get the values,

Object.values(currencyList) 
// You can save Object.values(res?.data?.results || {}) into the currencyList too, 
// e.g. setCurrencyList(Object.values(res?.data?.results || {})

then it will become an array with all the values object. If you want to map it and pass into a component,

Object.values(currencyList).map(currency => <Component item={currency} key={currency.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