I build an app in React and I have this object from an API:
let currency = {
"success": true,
"timestamp": 1648656784,
"base": "EUR",
"date": "2022-03-30",
"rates": {
"AED": 4.09826,
"AFN": 98.736662,
"RON": 4.947456,
"USD": 1.11575,
"GBP": 0.848656
}
}
In my code, I have a dropdown list where I can choose what currency I want:
<select name='currencyOptions' onChange={(e) => showCurrency(e.target.value)}>
<option defaultValue='USD'>USD</option>
<option defaultValue="EUR">EUR</option>
<option defaultValue='RON'>RON</option>
<option defaultValue='GBP'>GBP</option>
</select>
When I choose another currency from my dropdown list, I try to get the currency from that object dynamically, but I don't know the syntax.
function showCurrency(curr) {
let products_list = [...products];
for (let i = 0; i < products_list.length; i ++) {
if(currentCurrency === 'EUR') {
products_list[i].price = products_list[i].price * currency[0].rates. ????;
} else {
console.log(currency[0].rates.curr);
products_list[i].price = products_list[i].price / currency[0].rates. ????;
}
}
}
<option value='USD'>USD</option>currency[0].rates[currentCurrency]