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>
)
res.data['results']is not array, but instead object with currency as keys. You should transform to array usingObject.values(res.data['results']and like that only object with structure like this{currencyName: 'UAE Dirham', id: 'AED'}will be in your array - basically only the values as method implies.