skip to Main Content

I have a huge json file with array of objects. I want to go through that array and find a specific object then I want to return value based on 1 of its values.

Here is myfunction so far:

async function getMatchData(matchId,summoner){
    
    const response = await fetch(`https://europe.api.riotgames.com/lol/match/v5/matches/${matchId}?api_key=`+key);
    
    var data = await response.json();
    
    let matchData= {
        victory:
        players: data.info.participants.map(element => obj = { team: element.teamId, champ: element.championName, champIcon: element.championName + '_0.jpg', summoner: element.summonerName, level: element.champLevel, build: { item1: element.item1, item2: element.item2, item3: element.item3, item4: element.item4, item5: element.item5, item6: element.item6, item0: element.item0 }, kills: element.kills, deaths: element.deaths, assists: element.assists, kda: (element.kills + element.assists) / element.deaths, cs: element.totalMinionsKilled + element.neutralMinionsKilled, victory: element.win })
    }
  
   return matchData;

And here is what relevant part of data would look like:

"metadata": {
        "dataVersion": "2",
        "matchId": "EUW1_6324729398",
    },
    "info": {
        "gameCreation": 1679399513246,
        "gameDuration": 106,
        "gameEndTimestamp": 1679399729794,
        "gameId": 6324729398,
        "gameMode": "CLASSIC",
        "participants": [{
                "puuid": "REWcFv1LhXs4Yfkoq9iD0ENCdZT-dCA__ThUz0wZZ6pDbpQE95SRJpPZVFQbZXYiS4OR6IMHdBpBsA",
                "summonerId": "QbkQflR7yU5XsQRuDS59HuTW9vP_heQYZBCleFV92OvEWiyWSwOoRuqX1w",
                "summonerLevel": 88,
                "summonerName": "Pridestalker",
                "win": true
            },...]
        }

I want victory to be = data.info.participant.win where summonerName=summoner.

2

Answers


  1. Chosen as BEST ANSWER
    victory:  data.info.participants[data.info.participants.findIndex((element) => element.summonerName=summoner)].win
    

    thats how i ended up doing it


  2. victory: data.info.participants.find(e => e.summonerName == summoner).win;
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search