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

557
Visualizações
Get data from Rest countries API v3 using filter method

I was trying to get the currency data from Rest countries API v3.1: https://restcountries.com/v3.1/all. What I was trying to do was get the name of the countries which have United States dollar as it's currency and print it in browser console.

I tried the following code :

let xhr = new XMLHttpRequest();
xhr.open("GET", "https://restcountries.com/v3.1/all")
xhr.send();
xhr.onload = () => {
    let data = JSON.parse(xhr.response)
    let answer = data.filter((dummy) => (dummy.currencies !== undefined))
    let realanswer = answer.filter((dollar) => (dollar.currencies.name === "United States dollar"))
    console.log(realanswer)
}

But I am getting an empty array in output with no data in it.

I tried to get the data I need with rest countries api v2: https://restcountries.com/v2/all and got the data I needed with following code:

let xhr = new XMLHttpRequest();
xhr.open("GET", "https://restcountries.com/v2/all")
xhr.send();
xhr.onload = () => {
    let data = JSON.parse(xhr.response)
    let answer = data.filter((dummy) => (dummy.currencies !== undefined))
    let realanswer = answer.filter((dollar) => (dollar.currencies[0].name === "United States dollar"))
    console.log(realanswer)

}

So can anyone tell me why I am not getting the data from Rest countries API v3.1? Also I would like to inform that in v2.0 the currency data is stored in an array but in 3.1 it is stored in an object. I can't figure out how to access currencies data with v3.1. Also I want to get the data with the help of filter method only. Please help!!

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

 // Hello Check this out it works absolutely fine
let xhr = new XMLHttpRequest();
xhr.open("GET", "https://restcountries.com/v3.1/all")
xhr.send();
xhr.onload = () => {
    let data = JSON.parse(xhr.response)
     /* console.log(data) */
    let answer = data.filter((dummy) => (dummy.currencies !== undefined))
    console.log(answer.length)
    let realanswer = answer.filter((data) =>  {
    for (let key in data.currencies) {
        if(data.currencies[key].name === "United States dollar"){
      return data
      }
    }
    })
    console.log(realanswer.length, realanswer[0].currencies)
}

about 4 years ago · Juan Pablo Isaza Relatório

0

It's because in v3.1 it's not just currencies.name, it's like this:

enter image description here

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