So, basically you have setup data like:
convertFrom: {
currency: "GBP: British Pounds",
symbol: "",
amount: 0
},
convertTo: {
currency: "USD: United States Dollar",
symbol: "",
amount: 0
}
Next, inside convert() method you have also set the currency symbol like:
this.convertFrom.symbol = this.convertFrom.currency.toString().split(":")[0];
this.convertTo.symbol = this.convertTo.currency.toString().split(":")[0];
So, now values of convertFrom.symbol and convertTo.symbol are:
this.convertFrom.symbol => "GBP"
this.convertTo.symbol => "USD"
You have also mentioned fetch response here is like:
{
"success": true,
"base": "EUR",
"rates": {
"GBP": 0.869339,
"USD": 1.0875
}
}
Now, we easily access an object's properties by using the dot notation or the bracket notation. But as the object rates keys here are dynamic, we will need to use the bracket notation like:
const from = res.rates[this.convertFrom.symbol]
const to = res.rates[this.convertTo.symbol]
Demo:
const res = {
"success": true,
"base": "EUR",
"rates": {
"GBP": 0.869339,
"USD": 1.0875
}
}
const data = {
convertFrom: { symbol: "GBP" },
convertTo: { symbol: "USD" }
}
const from = res.rates[data.convertFrom.symbol];
const to = res.rates[data.convertTo.symbol];
console.log('From:\t', from)
console.log('To:\t', to)