2

I got some troubles with arrays. I'm trying to build my own lol-site. I'm using node, async, express and ejs. When i call an URL i got a response (see below). The problem is that i don't really know how to fetch data i want. It would be easier, but this JSON is not always looking the same way.

{
   "playerStatSummaries": [
      {
         "playerStatSummaryType": "CAP5x5",
         "aggregatedStats": {
            "totalNeutralMinionsKilled": 2042,
            "totalMinionKills": 4317,
            "totalChampionKills": 350,
            "totalAssists": 417,
            "totalTurretsKilled": 36
         },
         "modifyDate": 1453247261000,
         "wins": 20
      },
      {
         "playerStatSummaryType": "OdinUnranked",
         "aggregatedStats": {
            "averageNodeCaptureAssist": 1,
            "maxNodeNeutralizeAssist": 5,
            "maxChampionsKilled": 24,
            "totalChampionKills": 4577,
            "totalAssists": 4732,
            "averageChampionsKilled": 8,
            "averageNodeCapture": 5,
            "averageNumDeaths": 6,
            "maxNodeNeutralize": 10,
            "averageNodeNeutralize": 3,
            "averageTeamObjective": 1,
            "averageTotalPlayerScore": 985,
            "maxNodeCapture": 12,
            "maxObjectivePlayerScore": 1226,
            "averageNodeNeutralizeAssist": 2,
            "averageAssists": 7,
            "maxTotalPlayerScore": 2218,
            "maxAssists": 29,
            "maxCombatPlayerScore": 1132,
            "averageCombatPlayerScore": 406,
            "maxNodeCaptureAssist": 4,
            "totalNodeCapture": 2166,
            "totalNodeNeutralize": 1455,
            "maxTeamObjective": 2,
            "averageObjectivePlayerScore": 573
         },
         "modifyDate": 1468740801000,
         "wins": 256
      },

      {
         "playerStatSummaryType": "Unranked",
         "aggregatedStats": {
            "totalNeutralMinionsKilled": 65540,
            "totalMinionKills": 392799,
            "totalChampionKills": 28967,
            "totalAssists": 31347,
            "totalTurretsKilled": 3007
         },
         "modifyDate": 1480708148000,
         "wins": 1964
      },
      {
         "playerStatSummaryType": "RankedSolo5x5",
         "aggregatedStats": {
            "totalNeutralMinionsKilled": 5304,
            "totalMinionKills": 17234,
            "totalChampionKills": 935,
            "totalAssists": 2276,
            "totalTurretsKilled": 125
         },
         "losses": 96,
         "modifyDate": 1480808818000,
         "wins": 101
      }
   ],
   "summonerId": 'xxxxx'
}

I was trying to use but it's not working the way i want it:

var json = JSON.parse(body);
  for(var m=0; m<json['playerStatSummaries'].length; m++){
    s.push(json['playerStatSummaries'][m].playerStatSummaryType, json['playerStatSummaries'][m].wins);
};

And it's giving me back:

[ 'CAP5x5',
  20,
  'OdinUnranked',
  256,
  'Unranked',
  1964,
  'RankedSolo5x5',
  101 ]

I would like to get Unranked and 1964 from the first array. I don't know what to do with it. I would like to fecth (from first array) playerStatSummaryType Unranked and wins.

Basic

2 Answers 2

1

I would recommend you to structure the end result into a Object rather than array, with object you have more access control. See the below example.

var json = {
  "playerStatSummaries": [{
      "playerStatSummaryType": "CAP5x5",
      "aggregatedStats": {
        "totalNeutralMinionsKilled": 2042,
        "totalMinionKills": 4317,
        "totalChampionKills": 350,
        "totalAssists": 417,
        "totalTurretsKilled": 36
      },
      "modifyDate": 1453247261000,
      "wins": 20
    }, {
      "playerStatSummaryType": "OdinUnranked",
      "aggregatedStats": {
        "averageNodeCaptureAssist": 1,
        "maxNodeNeutralizeAssist": 5,
        "maxChampionsKilled": 24,
        "totalChampionKills": 4577,
        "totalAssists": 4732,
        "averageChampionsKilled": 8,
        "averageNodeCapture": 5,
        "averageNumDeaths": 6,
        "maxNodeNeutralize": 10,
        "averageNodeNeutralize": 3,
        "averageTeamObjective": 1,
        "averageTotalPlayerScore": 985,
        "maxNodeCapture": 12,
        "maxObjectivePlayerScore": 1226,
        "averageNodeNeutralizeAssist": 2,
        "averageAssists": 7,
        "maxTotalPlayerScore": 2218,
        "maxAssists": 29,
        "maxCombatPlayerScore": 1132,
        "averageCombatPlayerScore": 406,
        "maxNodeCaptureAssist": 4,
        "totalNodeCapture": 2166,
        "totalNodeNeutralize": 1455,
        "maxTeamObjective": 2,
        "averageObjectivePlayerScore": 573
      },
      "modifyDate": 1468740801000,
      "wins": 256
    },

    {
      "playerStatSummaryType": "Unranked",
      "aggregatedStats": {
        "totalNeutralMinionsKilled": 65540,
        "totalMinionKills": 392799,
        "totalChampionKills": 28967,
        "totalAssists": 31347,
        "totalTurretsKilled": 3007
      },
      "modifyDate": 1480708148000,
      "wins": 1964
    }, {
      "playerStatSummaryType": "RankedSolo5x5",
      "aggregatedStats": {
        "totalNeutralMinionsKilled": 5304,
        "totalMinionKills": 17234,
        "totalChampionKills": 935,
        "totalAssists": 2276,
        "totalTurretsKilled": 125
      },
      "losses": 96,
      "modifyDate": 1480808818000,
      "wins": 101
    }
  ],
  "summonerId": 'xxxxx'
}


var s = {}; // object
for (var m = 0; m < json['playerStatSummaries'].length; m++) {
  s[json['playerStatSummaries'][m].playerStatSummaryType] = {     
      wins: json['playerStatSummaries'][m].wins
  };
}

console.log(s);
alert("Unranked wins: "+ s["Unranked"].wins); //easy to fetch values

Sign up to request clarification or add additional context in comments.

3 Comments

The end result is still an array, but an array of objects rather than an array of strings.
@SébastienVercammen the end result is an Object with each property being a object. Note: there is no [ ] brackets
Downvoters Please care to comment my mistake.. :) let me learn too.
1

You could loop the result and only add the types you want:

var json = {
   "playerStatSummaries": [
      {
         "playerStatSummaryType": "CAP5x5",
         "aggregatedStats": {
            "totalNeutralMinionsKilled": 2042,
            "totalMinionKills": 4317,
            "totalChampionKills": 350,
            "totalAssists": 417,
            "totalTurretsKilled": 36
         },
         "modifyDate": 1453247261000,
         "wins": 20
      },
      {
         "playerStatSummaryType": "OdinUnranked",
         "aggregatedStats": {
            "averageNodeCaptureAssist": 1,
            "maxNodeNeutralizeAssist": 5,
            "maxChampionsKilled": 24,
            "totalChampionKills": 4577,
            "totalAssists": 4732,
            "averageChampionsKilled": 8,
            "averageNodeCapture": 5,
            "averageNumDeaths": 6,
            "maxNodeNeutralize": 10,
            "averageNodeNeutralize": 3,
            "averageTeamObjective": 1,
            "averageTotalPlayerScore": 985,
            "maxNodeCapture": 12,
            "maxObjectivePlayerScore": 1226,
            "averageNodeNeutralizeAssist": 2,
            "averageAssists": 7,
            "maxTotalPlayerScore": 2218,
            "maxAssists": 29,
            "maxCombatPlayerScore": 1132,
            "averageCombatPlayerScore": 406,
            "maxNodeCaptureAssist": 4,
            "totalNodeCapture": 2166,
            "totalNodeNeutralize": 1455,
            "maxTeamObjective": 2,
            "averageObjectivePlayerScore": 573
         },
         "modifyDate": 1468740801000,
         "wins": 256
      },

      {
         "playerStatSummaryType": "Unranked",
         "aggregatedStats": {
            "totalNeutralMinionsKilled": 65540,
            "totalMinionKills": 392799,
            "totalChampionKills": 28967,
            "totalAssists": 31347,
            "totalTurretsKilled": 3007
         },
         "modifyDate": 1480708148000,
         "wins": 1964
      },
      {
         "playerStatSummaryType": "RankedSolo5x5",
         "aggregatedStats": {
            "totalNeutralMinionsKilled": 5304,
            "totalMinionKills": 17234,
            "totalChampionKills": 935,
            "totalAssists": 2276,
            "totalTurretsKilled": 125
         },
         "losses": 96,
         "modifyDate": 1480808818000,
         "wins": 101
      }
   ],
   "summonerId": "xxxxxx"
};

//
var allowed_types = ["Unranked", "RankedSolo5x5"];

var summaries = json["playerStatSummaries"];
var len = summaries.length;

var results = [];

for (let i = 0; i < len; i++) {
  let summary = summaries[i];
  let type = summary["playerStatSummaryType"];
  
  if (allowed_types.indexOf(type) !== -1) {
    results.push({
      type: type,
      wins: summary["wins"]
    });
  }
}

console.log(results);

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.