Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

202
Visualizações
Access to child of

am using API which allows me to collect data about countries. If i want get data about languages and currencies from different countries there is a problem because for example currencies in Poland has

currencies: PLN: {name: 'Polish złoty', symbol: 'zł'}

and for Portuguesa has

EUR: {name: 'Euro', symbol: '€'}.

How can i get data about currencies by code below no matter to countries? (In code i have written 'THERE IS A PROBLEM WITH CURRENCIES' this are places which i have repair.)

const btn = document.querySelector('.btn-country');
const countriesContainer = document.querySelector('.countries');

///////////////////////////////////////
const getCountryData = function (country) {
  const request = new XMLHttpRequest();
  request.open(
    'GET',
    `https://restcountries.com/v3.1/name/${country}?fullText=true`
  );
  request.send();

  request.addEventListener('load', function () {
    console.log(this.responseText);

    const [data] = JSON.parse(this.responseText);
    console.log(data);

    const html = `
  <article class="country">
    <img class="country__img" src="${data.flags.svg}" />
      <div class="country__data">
        <h3 class="country__name">${data.name.common}</h3>
        <h4 class="country__region">${data.region}</h4>
        <p class="country__row"><span>👫</span>${(
          +data.population / 1000000
        ).toFixed(1)} people</p>
        <p class="country__row"><span>🗣️</span>${
          THERE IS A PROBLEM WITH LANGUAGE
        }</p>
        <p class="country__row"><span>💰</span>${THERE IS A PROBLEM WITH CURRENCIES}</p>
      </div>
    </article>
    `;

    countriesContainer.insertAdjacentHTML('beforeEnd', html);
    countriesContainer.style.opacity = 1;
  });
};
getCountryData('portugal');
getCountryData('poland');
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

You have to do pretty hacky things to achieve this. I've come with this way, if you want to extract it directly from the API response:

const currencyName = data[0].currencies[Object.keys(data[0].currencies)[0]].name;

A bit more readable:

const currencies = data[0].currencies;
const currenciesKeys = Object.keys(currencies);
const currencyName = currencies[currenciesKeys[0]].name;

We're getting the first object of the array received and access the currencies key. Then, we get the subkeys of currencies as an array and access the first one, and we retrieve the name key over it.

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda