我正在尝试浏览一些JSON数组,其中某些对象的大小可能会有所不同。 JSON片段太大而无法发布,因此我将其发布在JSON Blob上, HERE ,我正在尝试通过此结构并抓取其中一个具有最高值的对象属性。
在下面的图片中,我试图抓住"冠军" " totalSessionsPlayed"中具有最高编号的对象。
我的问题是弄清楚如何循环,然后捕获总会话值,然后比较它们并最终检索具有最高总会话数的冠军对象。我习惯于总是硬编码值/路径,因为它总是静态地设置大小,而在这里大小可以改变。
我相信我需要做以下事情...但我不完全确定
json = JSON && JSON.parse(response) || $.parseJSON(response);
$.each(json, function (i, el) {
if (el.stats.totalSessionsPlayed > highestSession)
{
highestSession = el.stats.totalSessionsPlayed...?
}
});
//when done, somehow get the champion object that relates to that highestSession value.
我还是JSON的新手,我只是迷失了遍历这个动态大小的JSON。
答案 0 :(得分:1)
>
获得totalSessionsPlayed
值最高的冠军;并使用===
确定是否已经有一个具有相同最高分的不同冠军。解决方案代码:
var highestSession = -1; // set it low initially
var topChamps = []; // array since could be a N-way tie
$.each(json.champions, function(idx, champion) {
if (champion.id > 0 && champion.stats) {
if (champion.stats.totalSessionsPlayed > highestSession) {
// new top score broke any possible ties
topChamps = [champion]; // new array
highestSession = champion.stats.totalSessionsPlayed; // save new max value
} else if (champion.stats.totalSessionsPlayed === highestSession) {
// it's a tie, add to list
topChamps[topChamps.length] = champion;
}
}
});
针对OP的实际JSON数据运行此示例代码段:
// fake json for example only
var json = {
"summonerId": 22421317,
"modifyDate": 1414692614000,
"champions": [{
"id": 110,
"stats": {
"totalSessionsPlayed": 5,
"totalSessionsLost": 5,
"totalSessionsWon": 0,
"totalChampionKills": 32,
"totalDamageDealt": 725106,
"totalDamageTaken": 113405,
"mostChampionKillsPerSession": 13,
"totalMinionKills": 937,
"totalDoubleKills": 3,
"totalTripleKills": 0,
"totalQuadraKills": 0,
"totalPentaKills": 0,
"totalUnrealKills": 0,
"totalDeathsPerSession": 52,
"totalGoldEarned": 62141,
"mostSpellsCast": 0,
"totalTurretsKilled": 6,
"totalPhysicalDamageDealt": 613669,
"totalMagicDamageDealt": 105253,
"totalFirstBlood": 0,
"totalAssists": 55,
"maxChampionsKilled": 13,
"maxNumDeaths": 14
}
}, {
"id": 21,
"stats": {
"totalSessionsPlayed": 7,
"totalSessionsLost": 1,
"totalSessionsWon": 6,
"totalChampionKills": 54,
"totalDamageDealt": 1143509,
"totalDamageTaken": 132528,
"mostChampionKillsPerSession": 18,
"totalMinionKills": 1420,
"totalDoubleKills": 7,
"totalTripleKills": 1,
"totalQuadraKills": 0,
"totalPentaKills": 0,
"totalUnrealKills": 0,
"totalDeathsPerSession": 56,
"totalGoldEarned": 99924,
"mostSpellsCast": 0,
"totalTurretsKilled": 24,
"totalPhysicalDamageDealt": 973400,
"totalMagicDamageDealt": 159071,
"totalFirstBlood": 0,
"totalAssists": 67,
"maxChampionsKilled": 18,
"maxNumDeaths": 11
}
}, {
"id": 19,
"stats": {
"totalSessionsPlayed": 18,
"totalSessionsLost": 9,
"totalSessionsWon": 9,
"totalChampionKills": 94,
"totalDamageDealt": 1864380,
"totalDamageTaken": 764921,
"mostChampionKillsPerSession": 12,
"totalMinionKills": 923,
"totalDoubleKills": 3,
"totalTripleKills": 0,
"totalQuadraKills": 0,
"totalPentaKills": 0,
"totalUnrealKills": 0,
"totalDeathsPerSession": 154,
"totalGoldEarned": 212650,
"mostSpellsCast": 0,
"totalTurretsKilled": 14,
"totalPhysicalDamageDealt": 932921,
"totalMagicDamageDealt": 565418,
"totalFirstBlood": 0,
"totalAssists": 241,
"maxChampionsKilled": 12,
"maxNumDeaths": 12
}
}, {
"id": 89,
"stats": {
"totalSessionsPlayed": 41,
"totalSessionsLost": 15,
"totalSessionsWon": 26,
"totalChampionKills": 162,
"totalDamageDealt": 1655899,
"totalDamageTaken": 1322562,
"mostChampionKillsPerSession": 12,
"totalMinionKills": 1684,
"totalDoubleKills": 6,
"totalTripleKills": 1,
"totalQuadraKills": 0,
"totalPentaKills": 0,
"totalUnrealKills": 0,
"totalDeathsPerSession": 318,
"totalGoldEarned": 447598,
"mostSpellsCast": 0,
"totalTurretsKilled": 16,
"totalPhysicalDamageDealt": 434852,
"totalMagicDamageDealt": 1042032,
"totalFirstBlood": 0,
"totalAssists": 716,
"maxChampionsKilled": 12,
"maxNumDeaths": 14
}
}, {
"id": 9,
"stats": {
"totalSessionsPlayed": 1,
"totalSessionsLost": 1,
"totalSessionsWon": 0,
"totalChampionKills": 14,
"totalDamageDealt": 269323,
"totalDamageTaken": 43294,
"mostChampionKillsPerSession": 14,
"totalMinionKills": 91,
"totalDoubleKills": 2,
"totalTripleKills": 0,
"totalQuadraKills": 0,
"totalPentaKills": 0,
"totalUnrealKills": 0,
"totalDeathsPerSession": 6,
"totalGoldEarned": 18899,
"mostSpellsCast": 0,
"totalTurretsKilled": 1,
"totalPhysicalDamageDealt": 17923,
"totalMagicDamageDealt": 236252,
"totalFirstBlood": 0,
"totalAssists": 18,
"maxChampionsKilled": 14,
"maxNumDeaths": 6
}
}, {
"id": 10,
"stats": {
"totalSessionsPlayed": 2,
"totalSessionsLost": 1,
"totalSessionsWon": 1,
"totalChampionKills": 14,
"totalDamageDealt": 229398,
"totalDamageTaken": 50962,
"mostChampionKillsPerSession": 13,
"totalMinionKills": 250,
"totalDoubleKills": 2,
"totalTripleKills": 0,
"totalQuadraKills": 0,
"totalPentaKills": 0,
"totalUnrealKills": 0,
"totalDeathsPerSession": 16,
"totalGoldEarned": 22793,
"mostSpellsCast": 0,
"totalTurretsKilled": 3,
"totalPhysicalDamageDealt": 69735,
"totalMagicDamageDealt": 157448,
"totalFirstBlood": 0,
"totalAssists": 12,
"maxChampionsKilled": 13,
"maxNumDeaths": 12
}
}, {
"id": 114,
"stats": {
"totalSessionsPlayed": 2,
"totalSessionsLost": 0,
"totalSessionsWon": 2,
"totalChampionKills": 10,
"totalDamageDealt": 216804,
"totalDamageTaken": 38006,
"mostChampionKillsPerSession": 6,
"totalMinionKills": 334,
"totalDoubleKills": 2,
"totalTripleKills": 0,
"totalQuadraKills": 0,
"totalPentaKills": 0,
"totalUnrealKills": 0,
"totalDeathsPerSession": 12,
"totalGoldEarned": 20796,
"mostSpellsCast": 0,
"totalTurretsKilled": 8,
"totalPhysicalDamageDealt": 211956,
"totalMagicDamageDealt": 2482,
"totalFirstBlood": 0,
"totalAssists": 9,
"maxChampionsKilled": 6,
"maxNumDeaths": 9
}
}, {
"id": 50,
"stats": {
"totalSessionsPlayed": 1,
"totalSessionsLost": 1,
"totalSessionsWon": 0,
"totalChampionKills": 7,
"totalDamageDealt": 58537,
"totalDamageTaken": 31729,
"mostChampionKillsPerSession": 7,
"totalMinionKills": 62,
"totalDoubleKills": 1,
"totalTripleKills": 0,
"totalQuadraKills": 0,
"totalPentaKills": 0,
"totalUnrealKills": 0,
"totalDeathsPerSession": 13,
"totalGoldEarned": 8280,
"mostSpellsCast": 0,
"totalTurretsKilled": 0,
"totalPhysicalDamageDealt": 11119,
"totalMagicDamageDealt": 46016,
"totalFirstBlood": 0,
"totalAssists": 7,
"maxChampionsKilled": 7,
"maxNumDeaths": 13
}
}, {
"id": 122,
"stats": {
"totalSessionsPlayed": 1,
"totalSessionsLost": 0,
"totalSessionsWon": 1,
"totalChampionKills": 5,
"totalDamageDealt": 102450,
"totalDamageTaken": 21890,
"mostChampionKillsPerSession": 5,
"totalMinionKills": 142,
"totalDoubleKills": 0,
"totalTripleKills": 0,
"totalQuadraKills": 0,
"totalPentaKills": 0,
"totalUnrealKills": 0,
"totalDeathsPerSession": 4,
"totalGoldEarned": 11038,
"mostSpellsCast": 0,
"totalTurretsKilled": 4,
"totalPhysicalDamageDealt": 91836,
"totalMagicDamageDealt": 7028,
"totalFirstBlood": 0,
"totalAssists": 9,
"maxChampionsKilled": 5,
"maxNumDeaths": 4
}
}, {
"id": 33,
"stats": {
"totalSessionsPlayed": 9,
"totalSessionsLost": 5,
"totalSessionsWon": 4,
"totalChampionKills": 30,
"totalDamageDealt": 808061,
"totalDamageTaken": 240939,
"mostChampionKillsPerSession": 9,
"totalMinionKills": 277,
"totalDoubleKills": 3,
"totalTripleKills": 0,
"totalQuadraKills": 0,
"totalPentaKills": 0,
"totalUnrealKills": 0,
"totalDeathsPerSession": 53,
"totalGoldEarned": 82246,
"mostSpellsCast": 0,
"totalTurretsKilled": 2,
"totalPhysicalDamageDealt": 263466,
"totalMagicDamageDealt": 354351,
"totalFirstBlood": 0,
"totalAssists": 95,
"maxChampionsKilled": 9,
"maxNumDeaths": 11
}
}, {
"id": 80,
"stats": {
"totalSessionsPlayed": 13,
"totalSessionsLost": 8,
"totalSessionsWon": 5,
"totalChampionKills": 91,
"totalDamageDealt": 1471756,
"totalDamageTaken": 309620,
"mostChampionKillsPerSession": 13,
"totalMinionKills": 1757,
"totalDoubleKills": 10,
"totalTripleKills": 1,
"totalQuadraKills": 0,
"totalPentaKills": 0,
"totalUnrealKills": 0,
"totalDeathsPerSession": 125,
"totalGoldEarned": 140885,
"mostSpellsCast": 0,
"totalTurretsKilled": 24,
"totalPhysicalDamageDealt": 1372131,
"totalMagicDamageDealt": 84732,
"totalFirstBlood": 0,
"totalAssists": 83,
"maxChampionsKilled": 13,
"maxNumDeaths": 19
}
}, {
"id": 63,
"stats": {
"totalSessionsPlayed": 1,
"totalSessionsLost": 0,
"totalSessionsWon": 1,
"totalChampionKills": 7,
"totalDamageDealt": 84633,
"totalDamageTaken": 10389,
"mostChampionKillsPerSession": 7,
"totalMinionKills": 98,
"totalDoubleKills": 1,
"totalTripleKills": 0,
"totalQuadraKills": 0,
"totalPentaKills": 0,
"totalUnrealKills": 0,
"totalDeathsPerSession": 2,
"totalGoldEarned": 9926,
"mostSpellsCast": 0,
"totalTurretsKilled": 0,
"totalPhysicalDamageDealt": 16719,
"totalMagicDamageDealt": 66391,
"totalFirstBlood": 0,
"totalAssists": 6,
"maxChampionsKilled": 7,
"maxNumDeaths": 2
}
}, {
"id": 32,
"stats": {
"totalSessionsPlayed": 17,
"totalSessionsLost": 9,
"totalSessionsWon": 8,
"totalChampionKills": 100,
"totalDamageDealt": 2324822,
"totalDamageTaken": 511432,
"mostChampionKillsPerSession": 13,
"totalMinionKills": 830,
"totalDoubleKills": 9,
"totalTripleKills": 1,
"totalQuadraKills": 0,
"totalPentaKills": 0,
"totalUnrealKills": 0,
"totalDeathsPerSession": 117,
"totalGoldEarned": 194949,
"mostSpellsCast": 0,
"totalTurretsKilled": 0,
"totalPhysicalDamageDealt": 249779,
"totalMagicDamageDealt": 1833229,
"totalFirstBlood": 0,
"totalAssists": 208,
"maxChampionsKilled": 13,
"maxNumDeaths": 10
}
}, {
"id": 31,
"stats": {
"totalSessionsPlayed": 11,
"totalSessionsLost": 6,
"totalSessionsWon": 5,
"totalChampionKills": 73,
"totalDamageDealt": 1565889,
"totalDamageTaken": 346947,
"mostChampionKillsPerSession": 16,
"totalMinionKills": 667,
"totalDoubleKills": 4,
"totalTripleKills": 1,
"totalQuadraKills": 0,
"totalPentaKills": 0,
"totalUnrealKills": 0,
"totalDeathsPerSession": 59,
"totalGoldEarned": 122954,
"mostSpellsCast": 0,
"totalTurretsKilled": 4,
"totalPhysicalDamageDealt": 299979,
"totalMagicDamageDealt": 940284,
"totalFirstBlood": 0,
"totalAssists": 98,
"maxChampionsKilled": 16,
"maxNumDeaths": 8
}
}, {
"id": 0,
"stats": {
"totalSessionsPlayed": 129,
"totalSessionsLost": 61,
"totalSessionsWon": 68,
"totalChampionKills": 693,
"killingSpree": 285,
"totalDamageDealt": 12520567,
"totalDamageTaken": 3938624,
"mostChampionKillsPerSession": 18,
"totalMinionKills": 9472,
"totalDoubleKills": 53,
"totalTripleKills": 5,
"totalQuadraKills": 0,
"totalPentaKills": 0,
"totalUnrealKills": 0,
"totalDeathsPerSession": 987,
"totalGoldEarned": 1455079,
"mostSpellsCast": 0,
"totalTurretsKilled": 106,
"totalPhysicalDamageDealt": 5559485,
"totalMagicDamageDealt": 5599987,
"totalNeutralMinionsKilled": 3748,
"totalFirstBlood": 0,
"totalAssists": 1624,
"totalHeal": 601627,
"maxLargestKillingSpree": 9,
"maxLargestCriticalStrike": 925,
"maxChampionsKilled": 18,
"maxNumDeaths": 19,
"maxTimePlayed": 3633,
"maxTimeSpentLiving": 1504,
"normalGamesPlayed": 0,
"rankedSoloGamesPlayed": 0,
"rankedPremadeGamesPlayed": 0,
"botGamesPlayed": 0
}
}]
}
var highestSession = -1; // set it low initially
var topChamps = []; // array since could be a N-way tie
$.each(json.champions, function(idx, champion) {
if (champion.id > 0 && champion.stats) {
if (champion.stats.totalSessionsPlayed > highestSession) {
// new top score broke any possible ties
topChamps = [champion];
highestSession = champion.stats.totalSessionsPlayed; // save new max value
} else if (champion.stats.totalSessionsPlayed === highestSession) {
// it's a tie, add to list
topChamps[topChamps.length] = champion;
}
}
});
$(document.body).append("<div>TOP CHAMP ID(s): " + topChamps.map(function(champ) {
return champ.id
}).join(", ") + "</div>");
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
答案 1 :(得分:0)
您需要做的只是减少阵列。您根本不需要jQuery,因为forEach
,map
和reduce
是JavaScript中内置于Arrays的基本本机操作。
var bestChamp = json.champions.reduce(function (prev, next) {
if (prev.stats.totalSessionsPlayed >= next.stats.totalSessionsPlayed) {
return prev;
} else {
return next;
}
});
// bestChamp contains the object
reduce
将数组转换为单个值/对象。因此,在这种情况下,我们将champions
数组转换为一个对象,其中totalSessionsPlayed
的数量最多。
您可以阅读有关Array.prototype.reduce
on MDN
如果您还想要使用id = 0
排除对象,那么就像将此条件添加到if
语句一样简单:
var bestChamp = json.champions.reduce(function (prev, next) {
if (next.id === 0) {
return prev;
}
if (prev.stats.totalSessionsPlayed >= next.stats.totalSessionsPlayed) {
return prev;
} else {
return next;
}
});
// bestChamp contains the object
以下是整个片段:
var json = {
"summonerId": 22421317,
"modifyDate": 1414692614000,
"champions": [
{
"id": 110,
"stats": {
"totalSessionsPlayed": 5,
"totalSessionsLost": 5,
"totalSessionsWon": 0,
"totalChampionKills": 32,
"totalDamageDealt": 725106,
"totalDamageTaken": 113405,
"mostChampionKillsPerSession": 13,
"totalMinionKills": 937,
"totalDoubleKills": 3,
"totalTripleKills": 0,
"totalQuadraKills": 0,
"totalPentaKills": 0,
"totalUnrealKills": 0,
"totalDeathsPerSession": 52,
"totalGoldEarned": 62141,
"mostSpellsCast": 0,
"totalTurretsKilled": 6,
"totalPhysicalDamageDealt": 613669,
"totalMagicDamageDealt": 105253,
"totalFirstBlood": 0,
"totalAssists": 55,
"maxChampionsKilled": 13,
"maxNumDeaths": 14
}
},
{
"id": 21,
"stats": {
"totalSessionsPlayed": 7,
"totalSessionsLost": 1,
"totalSessionsWon": 6,
"totalChampionKills": 54,
"totalDamageDealt": 1143509,
"totalDamageTaken": 132528,
"mostChampionKillsPerSession": 18,
"totalMinionKills": 1420,
"totalDoubleKills": 7,
"totalTripleKills": 1,
"totalQuadraKills": 0,
"totalPentaKills": 0,
"totalUnrealKills": 0,
"totalDeathsPerSession": 56,
"totalGoldEarned": 99924,
"mostSpellsCast": 0,
"totalTurretsKilled": 24,
"totalPhysicalDamageDealt": 973400,
"totalMagicDamageDealt": 159071,
"totalFirstBlood": 0,
"totalAssists": 67,
"maxChampionsKilled": 18,
"maxNumDeaths": 11
}
},
{
"id": 19,
"stats": {
"totalSessionsPlayed": 18,
"totalSessionsLost": 9,
"totalSessionsWon": 9,
"totalChampionKills": 94,
"totalDamageDealt": 1864380,
"totalDamageTaken": 764921,
"mostChampionKillsPerSession": 12,
"totalMinionKills": 923,
"totalDoubleKills": 3,
"totalTripleKills": 0,
"totalQuadraKills": 0,
"totalPentaKills": 0,
"totalUnrealKills": 0,
"totalDeathsPerSession": 154,
"totalGoldEarned": 212650,
"mostSpellsCast": 0,
"totalTurretsKilled": 14,
"totalPhysicalDamageDealt": 932921,
"totalMagicDamageDealt": 565418,
"totalFirstBlood": 0,
"totalAssists": 241,
"maxChampionsKilled": 12,
"maxNumDeaths": 12
}
},
{
"id": 89,
"stats": {
"totalSessionsPlayed": 41,
"totalSessionsLost": 15,
"totalSessionsWon": 26,
"totalChampionKills": 162,
"totalDamageDealt": 1655899,
"totalDamageTaken": 1322562,
"mostChampionKillsPerSession": 12,
"totalMinionKills": 1684,
"totalDoubleKills": 6,
"totalTripleKills": 1,
"totalQuadraKills": 0,
"totalPentaKills": 0,
"totalUnrealKills": 0,
"totalDeathsPerSession": 318,
"totalGoldEarned": 447598,
"mostSpellsCast": 0,
"totalTurretsKilled": 16,
"totalPhysicalDamageDealt": 434852,
"totalMagicDamageDealt": 1042032,
"totalFirstBlood": 0,
"totalAssists": 716,
"maxChampionsKilled": 12,
"maxNumDeaths": 14
}
},
{
"id": 9,
"stats": {
"totalSessionsPlayed": 1,
"totalSessionsLost": 1,
"totalSessionsWon": 0,
"totalChampionKills": 14,
"totalDamageDealt": 269323,
"totalDamageTaken": 43294,
"mostChampionKillsPerSession": 14,
"totalMinionKills": 91,
"totalDoubleKills": 2,
"totalTripleKills": 0,
"totalQuadraKills": 0,
"totalPentaKills": 0,
"totalUnrealKills": 0,
"totalDeathsPerSession": 6,
"totalGoldEarned": 18899,
"mostSpellsCast": 0,
"totalTurretsKilled": 1,
"totalPhysicalDamageDealt": 17923,
"totalMagicDamageDealt": 236252,
"totalFirstBlood": 0,
"totalAssists": 18,
"maxChampionsKilled": 14,
"maxNumDeaths": 6
}
},
{
"id": 10,
"stats": {
"totalSessionsPlayed": 2,
"totalSessionsLost": 1,
"totalSessionsWon": 1,
"totalChampionKills": 14,
"totalDamageDealt": 229398,
"totalDamageTaken": 50962,
"mostChampionKillsPerSession": 13,
"totalMinionKills": 250,
"totalDoubleKills": 2,
"totalTripleKills": 0,
"totalQuadraKills": 0,
"totalPentaKills": 0,
"totalUnrealKills": 0,
"totalDeathsPerSession": 16,
"totalGoldEarned": 22793,
"mostSpellsCast": 0,
"totalTurretsKilled": 3,
"totalPhysicalDamageDealt": 69735,
"totalMagicDamageDealt": 157448,
"totalFirstBlood": 0,
"totalAssists": 12,
"maxChampionsKilled": 13,
"maxNumDeaths": 12
}
},
{
"id": 114,
"stats": {
"totalSessionsPlayed": 2,
"totalSessionsLost": 0,
"totalSessionsWon": 2,
"totalChampionKills": 10,
"totalDamageDealt": 216804,
"totalDamageTaken": 38006,
"mostChampionKillsPerSession": 6,
"totalMinionKills": 334,
"totalDoubleKills": 2,
"totalTripleKills": 0,
"totalQuadraKills": 0,
"totalPentaKills": 0,
"totalUnrealKills": 0,
"totalDeathsPerSession": 12,
"totalGoldEarned": 20796,
"mostSpellsCast": 0,
"totalTurretsKilled": 8,
"totalPhysicalDamageDealt": 211956,
"totalMagicDamageDealt": 2482,
"totalFirstBlood": 0,
"totalAssists": 9,
"maxChampionsKilled": 6,
"maxNumDeaths": 9
}
},
{
"id": 50,
"stats": {
"totalSessionsPlayed": 1,
"totalSessionsLost": 1,
"totalSessionsWon": 0,
"totalChampionKills": 7,
"totalDamageDealt": 58537,
"totalDamageTaken": 31729,
"mostChampionKillsPerSession": 7,
"totalMinionKills": 62,
"totalDoubleKills": 1,
"totalTripleKills": 0,
"totalQuadraKills": 0,
"totalPentaKills": 0,
"totalUnrealKills": 0,
"totalDeathsPerSession": 13,
"totalGoldEarned": 8280,
"mostSpellsCast": 0,
"totalTurretsKilled": 0,
"totalPhysicalDamageDealt": 11119,
"totalMagicDamageDealt": 46016,
"totalFirstBlood": 0,
"totalAssists": 7,
"maxChampionsKilled": 7,
"maxNumDeaths": 13
}
},
{
"id": 122,
"stats": {
"totalSessionsPlayed": 1,
"totalSessionsLost": 0,
"totalSessionsWon": 1,
"totalChampionKills": 5,
"totalDamageDealt": 102450,
"totalDamageTaken": 21890,
"mostChampionKillsPerSession": 5,
"totalMinionKills": 142,
"totalDoubleKills": 0,
"totalTripleKills": 0,
"totalQuadraKills": 0,
"totalPentaKills": 0,
"totalUnrealKills": 0,
"totalDeathsPerSession": 4,
"totalGoldEarned": 11038,
"mostSpellsCast": 0,
"totalTurretsKilled": 4,
"totalPhysicalDamageDealt": 91836,
"totalMagicDamageDealt": 7028,
"totalFirstBlood": 0,
"totalAssists": 9,
"maxChampionsKilled": 5,
"maxNumDeaths": 4
}
},
{
"id": 33,
"stats": {
"totalSessionsPlayed": 9,
"totalSessionsLost": 5,
"totalSessionsWon": 4,
"totalChampionKills": 30,
"totalDamageDealt": 808061,
"totalDamageTaken": 240939,
"mostChampionKillsPerSession": 9,
"totalMinionKills": 277,
"totalDoubleKills": 3,
"totalTripleKills": 0,
"totalQuadraKills": 0,
"totalPentaKills": 0,
"totalUnrealKills": 0,
"totalDeathsPerSession": 53,
"totalGoldEarned": 82246,
"mostSpellsCast": 0,
"totalTurretsKilled": 2,
"totalPhysicalDamageDealt": 263466,
"totalMagicDamageDealt": 354351,
"totalFirstBlood": 0,
"totalAssists": 95,
"maxChampionsKilled": 9,
"maxNumDeaths": 11
}
},
{
"id": 80,
"stats": {
"totalSessionsPlayed": 13,
"totalSessionsLost": 8,
"totalSessionsWon": 5,
"totalChampionKills": 91,
"totalDamageDealt": 1471756,
"totalDamageTaken": 309620,
"mostChampionKillsPerSession": 13,
"totalMinionKills": 1757,
"totalDoubleKills": 10,
"totalTripleKills": 1,
"totalQuadraKills": 0,
"totalPentaKills": 0,
"totalUnrealKills": 0,
"totalDeathsPerSession": 125,
"totalGoldEarned": 140885,
"mostSpellsCast": 0,
"totalTurretsKilled": 24,
"totalPhysicalDamageDealt": 1372131,
"totalMagicDamageDealt": 84732,
"totalFirstBlood": 0,
"totalAssists": 83,
"maxChampionsKilled": 13,
"maxNumDeaths": 19
}
},
{
"id": 63,
"stats": {
"totalSessionsPlayed": 1,
"totalSessionsLost": 0,
"totalSessionsWon": 1,
"totalChampionKills": 7,
"totalDamageDealt": 84633,
"totalDamageTaken": 10389,
"mostChampionKillsPerSession": 7,
"totalMinionKills": 98,
"totalDoubleKills": 1,
"totalTripleKills": 0,
"totalQuadraKills": 0,
"totalPentaKills": 0,
"totalUnrealKills": 0,
"totalDeathsPerSession": 2,
"totalGoldEarned": 9926,
"mostSpellsCast": 0,
"totalTurretsKilled": 0,
"totalPhysicalDamageDealt": 16719,
"totalMagicDamageDealt": 66391,
"totalFirstBlood": 0,
"totalAssists": 6,
"maxChampionsKilled": 7,
"maxNumDeaths": 2
}
},
{
"id": 32,
"stats": {
"totalSessionsPlayed": 17,
"totalSessionsLost": 9,
"totalSessionsWon": 8,
"totalChampionKills": 100,
"totalDamageDealt": 2324822,
"totalDamageTaken": 511432,
"mostChampionKillsPerSession": 13,
"totalMinionKills": 830,
"totalDoubleKills": 9,
"totalTripleKills": 1,
"totalQuadraKills": 0,
"totalPentaKills": 0,
"totalUnrealKills": 0,
"totalDeathsPerSession": 117,
"totalGoldEarned": 194949,
"mostSpellsCast": 0,
"totalTurretsKilled": 0,
"totalPhysicalDamageDealt": 249779,
"totalMagicDamageDealt": 1833229,
"totalFirstBlood": 0,
"totalAssists": 208,
"maxChampionsKilled": 13,
"maxNumDeaths": 10
}
},
{
"id": 31,
"stats": {
"totalSessionsPlayed": 11,
"totalSessionsLost": 6,
"totalSessionsWon": 5,
"totalChampionKills": 73,
"totalDamageDealt": 1565889,
"totalDamageTaken": 346947,
"mostChampionKillsPerSession": 16,
"totalMinionKills": 667,
"totalDoubleKills": 4,
"totalTripleKills": 1,
"totalQuadraKills": 0,
"totalPentaKills": 0,
"totalUnrealKills": 0,
"totalDeathsPerSession": 59,
"totalGoldEarned": 122954,
"mostSpellsCast": 0,
"totalTurretsKilled": 4,
"totalPhysicalDamageDealt": 299979,
"totalMagicDamageDealt": 940284,
"totalFirstBlood": 0,
"totalAssists": 98,
"maxChampionsKilled": 16,
"maxNumDeaths": 8
}
},
{
"id": 0,
"stats": {
"totalSessionsPlayed": 129,
"totalSessionsLost": 61,
"totalSessionsWon": 68,
"totalChampionKills": 693,
"killingSpree": 285,
"totalDamageDealt": 12520567,
"totalDamageTaken": 3938624,
"mostChampionKillsPerSession": 18,
"totalMinionKills": 9472,
"totalDoubleKills": 53,
"totalTripleKills": 5,
"totalQuadraKills": 0,
"totalPentaKills": 0,
"totalUnrealKills": 0,
"totalDeathsPerSession": 987,
"totalGoldEarned": 1455079,
"mostSpellsCast": 0,
"totalTurretsKilled": 106,
"totalPhysicalDamageDealt": 5559485,
"totalMagicDamageDealt": 5599987,
"totalNeutralMinionsKilled": 3748,
"totalFirstBlood": 0,
"totalAssists": 1624,
"totalHeal": 601627,
"maxLargestKillingSpree": 9,
"maxLargestCriticalStrike": 925,
"maxChampionsKilled": 18,
"maxNumDeaths": 19,
"maxTimePlayed": 3633,
"maxTimeSpentLiving": 1504,
"normalGamesPlayed": 0,
"rankedSoloGamesPlayed": 0,
"rankedPremadeGamesPlayed": 0,
"botGamesPlayed": 0
}
}
]
}
var bestChamp = json.champions.reduce(function (prev, next) {
if (next.id === 0) {
return prev;
}
if (prev.stats.totalSessionsPlayed >= next.stats.totalSessionsPlayed) {
return prev;
} else {
return next;
}
});
alert(bestChamp.id);
答案 2 :(得分:0)
如果你正在寻找更简单的东西,你可以考虑这个:
json.champions.reduce(function(current, champ) {
return champ.stats.totalSessionsPlayed > current.stats.totalSessionsPlayed
? champ : current;
});
它没有在@nothingisnecessary的答案中进行错误检查,也没有允许联系的可能性,只选择总数最高的第一个。但它的代码非常简单。