0

I'm fetching data from an API which returns currency exchange rates like so:

{"EUR_USD":1.044136}

I was able to manipulate the fetched data using:

const rate = Object.values(data).splice(11,5);

which returns 1.044

However, my next problem comes when trying to parseFloat this object into a number.. for some reason it only returns the first digit so in case of "1.044" it'd convert it into "1".

How can I handle this ? I'm thinking to perhaps map thru the rate array and then parseFloat everything..?

const parsedRate = rate.map(i => i).parseFloat(); would this even work ?

ok guys - sorry for taking your time and I appreciate all your answers! but I had JSON.stringify on the string and that's what was causing the problem... SORRY!!!

5
  • 1
    If that's how you're getting the data, why not just use data.EUR_USD rather than a strange splice call? Then it would already be a Number value... Commented Jun 29, 2022 at 20:14
  • well, there are multiple choices of currencies to convert.. should I just try to get each currency symbol and plug it into data.EUR_USD like so: ``` data.${currOne}_${currTwo} ``` i think this might be the best solution tbh, but I still need to find a way to parseFloat everything Commented Jun 29, 2022 at 20:17
  • 2
    Perhaps you could be more forthcoming with the actual form the data are coming in, and how you need to access the data. As mentioned, the data within the EUR_USD property is already a "float", so there's no need to parse it as one. Commented Jun 29, 2022 at 20:20
  • I want to do a currency rate exchange, for example let's say the rate is 1.044 ( as stated in the example ) - and someone wants to see how much 100 euro would be in USD - then it should convert it into 104.4 Commented Jun 29, 2022 at 20:22
  • 2
    Okay... That doesn't answer the request made, which is to tell us exactly what data you are getting back and how you are accessing it. Commented Jun 29, 2022 at 20:25

3 Answers 3

1

The example data you have supplied is an plain old Javascript object literal:

{"EUR_USD":1.044136}

You just need to access the exchange rate(s) by their name. Assuming it's in a variable called rates, it's just

const rate = rates.EUR_USD;

to assign the numeric value 1.044136 to the variable rate.

If, however, you have a string containing the JSON response from the API:

"{\"EUR_USD\":1.044136}"

And you're trying to use string.splice() to extract bits from it, you are going about it the wrong way.

All you have to do to rehydrate that JSON response body into an object is to use JSON.parse():

const json = callUpstreamExchangeRateApi();
const rates = JSON.parse(json);
const rate  = rates.EUR_USD;
Sign up to request clarification or add additional context in comments.

Comments

1

array.map(Number) would return an array of whatever is being mapped converted to number (assuming its possible)

2 Comments

1, NaN, 0, 4, 4 it's giving me this as of now.. I could potentially turn NaN into "." - however I'm still not sure if I'd be able to parseFloat it afterwards
Not sure what you are exactly trying to do, but if this is the approach you are sticking with, assuming you have an array of [1, '.', 0, 4, 4] you can then: Number([1, '.', 0, 4, 4].join('')) and you will get 1.044
1

I think you have problem with different length of keys. You have to use Object.values

const data = [{"EUR_NOUSD":"1.044136"}, {"EUR_EURO":"1.15"},{"EUR_CANDIES":"2.333"}]

const getValue = currency => Number(Object.values(currency))

console.log(data.map(getValue))

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.