0

I need to make function that returns name of champion by champions id. But first I need to search for the champion name inside of JSON file by champions id and then return the values from JSON file. Can anyone help me, please?

const champid = 21 //Champion id
console.log(champName(champid, leagueversions)) //This should return 'Miss Fortune' but retuns as: Promise { <pending> }

async function champName(champid, leagueversions)
{
const searchchamps = await axios.get(`http://ddragon.leagueoflegends.com/cdn/${leagueversions.champion}/data/en_US/champion.json`) //Getting all champions data from league
        let championList = searchchamps.data.data //JSON list of champions

        for (var i in championList) { 
            if(championList[i].key == champid) //if key value inside of json is same as champid
            {
                return championList[i].name //Should return name of champion
            }
        }
}
VARMOLORD
  • 3
  • 2
  • If you're only returning the first value, why don't you just return as soon as you find it, instead of pushing onto an array? – Barmar Aug 03 '21 at 01:09
  • `champName(champis, leagueversions)` will never return 'Miss Fortune'; it will return a Promise for 'Miss Fortune'. See the dupe. Also, just use [`find`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/find) – Heretic Monkey Aug 03 '21 at 01:09
  • `champName(champid, leagueversions).then(champ => console.log(champ))` – Barmar Aug 03 '21 at 01:10

0 Answers0