从数组中获取数据

时间:2016-12-05 12:35:24

标签: javascript arrays

我遇到了一些阵列问题。我正在尝试建立自己的lol网站。我正在使用node,async,express和ejs。当我呼叫URL时,我收到了回复(见下文)。问题是我真的不知道如何获取我想要的数据。它会更容易,但是这个JSON并不总是看起来一样。

<a id="Edit" class="button warning" data-open="editUnit{{ $unit->id }}">Edit</a>

我试图使用,但它不按我想要的方式工作:

{
   "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 json = JSON.parse(body);
  for(var m=0; m<json['playerStatSummaries'].length; m++){
    s.push(json['playerStatSummaries'][m].playerStatSummaryType, json['playerStatSummaries'][m].wins);
};
  

我想从第一个阵列获得Unranked和1964。我不知道该怎么做。我想fecth(从第一个数组)playerStatSummaryType Unranked并获胜。

基本

3 个答案:

答案 0 :(得分:1)

您可以循环结果并仅添加所需的类型:

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);

答案 1 :(得分:1)

我建议你将最终结果构造成一个Object 而不是数组,对象你有更多的访问控制。请参阅以下示例。

const reference

答案 2 :(得分:0)

这不是一个有效的json。访问此网址http://jsonviewer.stack.hu/,您可以在其中检查这是否是有效的json。

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