• Jobs
  • About Us
  • professionals
    • Home
    • Jobs
    • Courses and challenges
  • business
    • Home
    • Post vacancy
    • Our process
    • Pricing
    • Assessments
    • Payroll
    • Blog
    • Sales
    • Salary Calculator

0

163
Views
Is there some sort of wildcard when fetching data?

I want to display the name of the currency of different countries. To do this I use an API and fetch the json in my JS file.

this is the json response file I get calling Brazil

This is the json response file I get calling USA

The problem is the different names. In this case "BRL" oder "USD" to enter the names of the currencies.

my code to call the name of brazils currency

In this case I used "BRL", but I want to be able to enter every different countries currency element. I would think that I would need some sort of wildcard.

Maybe some of you can help me on how to handle this problem.

almost 3 years ago · Juan Pablo Isaza
3 answers
Answer question

0

I actually found out that there is an older version of the API in which the currency is display a lot simplier. That way I can access the currencies of each country easier.

In the older version the json for USA´s currency for example would look like this

That way I dont actually need to enter the different kinds of currencies first, when fetching the data.

almost 3 years ago · Juan Pablo Isaza Report

0

you can do it more simple way

document.querySelector("#currency").innerHTML = 
                          Object.values(data[0].currencies)[0].name;

or maybe this if you want to show several properties

function displayCountry(data) {
  data.forEach((item) => {
    Object.keys(item.currencies).forEach((key) => {
      document.querySelector("#currency").innerHTML = item.currencies[key].name;
    });
  });
};
almost 3 years ago · Juan Pablo Isaza Report

0

If you are calling the API with a currency abbreviation like "USD" and "BRL", then you can put those into an array like this:

const arrayOfCurrencies = ["USD", "BRL", ...];

When you are displaying them to the DOM, you can use those values as dynamic properties of the response like so:

for(let abbr of arrayOfCurrencies) {
  document.querySelector("#currency").innerHTML = data[0].currencies?.[abbr]?.name ?? fallbackString;
}
almost 3 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Our process Sales
Legal
Terms and conditions Privacy policy
© 2025 PeakU Inc. All Rights Reserved.

Andres GPT

Recommend me some offers
I have an error