Help me to display part of a API response

Hi!
I’m using the frankfurter currency converter API
I am playing with APIs and creating a simple currency converter.
This takes 3 inputs AMOUNT, FROM, TO.
const endPoint = 'https://frankfurter.app/latest?amount=10&from=GBP&to=EUR

I have successfully returned this from xhr.response - Inputted values: 10, from = GBP to = EUR.

{“amount”:10,“base”:“GBP”,“date”:“2019-05-23”,“rates”:{“EUR”:11.3507}}.

I can then console.log(xhr.response.rates.EUR) to get the EUR amount. This is only for this specific example. Is there any way to make this dynamic so that any to and from can be inputted.
Something like this?
‘xhr.response.rates.’+{to}

Hi,

You could try this (not sure if it is what you’re looking for):

let toCur = 'EUR';

console.log(xhr.response.rates[toCur]);

You’ll have to change the hyperlink too to the currency you want to return, something like this (just an example):

let endPoint;
let toCur = 'EUR';
let fromCur = 'GBP';

function setValues(amount) {
    endPoint = 'https://frankfurter.app/latest?amount=' + amount + '&from=' + fromCur + '&to=' + toCur;
}

setValues(10);

You can also add the to and from currencies to the input parameters of the function if you want.

2 Likes