0

I've got some troubles fetching data from JSON array. I'm using node + async + request (and express ofc).

Part of the code :

 function(data, callback){
                var URL = 'https://'+ server + '.api.pvp.net/api/lol/' + serw + '/v1.3/stats/by-summoner/' + data.id + '/summary?season=SEASON2016&api_key=' + api;
                request(URL, function(err, response, body){
                    if(!err & response.statusCode == 200){
                        var json = JSON.parse(body);
                        var assists = json['playerStatSummaries'][0]['aggregatedStats'].totalAssists;

The main problem is that [0] is giving back the first object from array.

{
   "playerStatSummaries": [
      {
         "playerStatSummaryType": "CAP5x5",
         "aggregatedStats": {
            "totalNeutralMinionsKilled": 2042,
            "totalMinionKills": 4317,
            "totalChampionKills": 350,
            "totalAssists": 417,
            "totalTurretsKilled": 36
         },
         "modifyDate": 1453276061000,
         "wins": 20
      },
      {
         "playerStatSummaryType": "CoopVsAI",
         "aggregatedStats": {
            "totalNeutralMinionsKilled": 446,
            "totalMinionKills": 6100,
            "totalChampionKills": 1092,
            "totalAssists": 761,
            "totalTurretsKilled": 116
         },
         "modifyDate": 1453276061000,
         "wins": 80
      },

Sometimes the array is a bit different and if player haven't played any "CAP5x5" game, the COOPvsAI is the [0] object.

Any ideas how to fetch data by it's playerStatSummaryType instead of object number in array? Or how to make some kind of bypass that makes null when can't find "CAP5x5" mode? Thanks.

2
  • What to you want to get out? Commented Jul 10, 2016 at 15:08
  • var assists = json['playerStatSummaries'][0]['aggregatedStats'].totalAssists; Gives me totalAssists of 1 object in the array and i want to get out totalAssists but without using [0], because sometimes if player hasn't played this mode the [0] is something totally different and it's not giving back data that i need. Commented Jul 10, 2016 at 15:09

2 Answers 2

1
function filterOnPlayerStatSummary(myObject, filter)
{
    var result = [];
    for(var i = 0; i < myObject.length; i++) 
    {
        if (myObject[i].playerStatSummary == filter)
        { 
            result.push(myObject[i]);
        }
    }

    return result;
}
Sign up to request clarification or add additional context in comments.

Comments

0

if you are using ES2015, the following will work

let json = JSON.parse(body);
let assists = json
   .playerStatSummaries
   .filter((a) => a.playerStatSummaryType =='CAP5x5')
   .map((a) => a.aggregatedStats.totalAssists)
   .reduce((a,b)=>a+b)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.