0

from the countries API I want to identify the 10 largests countries, already got the area values of these countries in the largest array but I want to reference the names of those cuntries in the object obtained from the API.

const countriesAPI = 'https://restcountries.com/v2/all'
let area = []
let names = []
let languages = []
let langNames = [] 
const fetchData = async () => {
    try {
      const response = await fetch(countriesAPI)
      const countries = await response.json()
      countries.forEach(country => {
          area.push(country.area)
          names.push(country.name)
          languages.push(country.languages)
      })
      let areaSorted = area.sort((function(a,b){return b - a}))
      let largests = areaSorted.slice(0,10)
      console.log(largests)
    } catch (err) {
      console.error(err)
    }
}
fetchData()

this is what I got so far

How can I use the info from the top ten areas to find what cuntries they belong to

  • Well don't put the areas, names and languages into different arrays. You're sorting only the areas, but that means you no longer know which area belongs to which country. Instead, sort the `countries` array itself. – Bergi Mar 04 '23 at 19:57
  • @Bergi thanks I tried that and resolved the bigger issue, now I am having a problem, the biggest should be Russia but with the sort method Antartica comes as the biggest, any idea on how to solve it? – Galo Flores Mar 04 '23 at 20:58

0 Answers0