Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

237
Views
¿Cómo mapear una matriz de objetos en reaccionar?

Tengo una variedad de monedas que me gustaría mapear. Pero no estoy seguro de cómo? Mi aplicación falla con el código que escribí y devuelve una página vacía con un error: currencyList.map is not a function

Esto es lo que obtengo cuando consola. registro los datos obtenidos:

 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'}

Hay muchas más entradas allí..

de todos modos este es mi código

 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 answers
Answer question

0

Por lo que entiendo, tienes un objeto, no una matriz como respuesta, algo así como:

 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'} }

Así que no hay mapa en los objetos. Necesitará obtener las claves, por ejemplo:

 Object.keys(currencyList).map( key => <div>{currencyList[key].id}</div> )
about 4 years ago · Juan Pablo Isaza Report

0

Si desea obtener los valores,

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

luego se convertirá en una matriz con todos los objetos de valores. Si desea mapearlo y pasar a un componente,

 Object.values(currencyList).map(currency => <Component item={currency} key={currency.id} />)
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!