- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试浏览一个 JSON 数组,其中某些对象的大小可能会有所不同。 JSON 片段太大而无法发布,因此我将其发布到 JSON Blob, HERE 并且我正在尝试遍历此结构并获取其属性之一中具有最高值的对象。
在下图中,我试图获取“totalSessionsPlayed”中数量最多的“champion”对象。
我的问题是弄清楚如何循环,然后捕获总 session 值,然后比较它们并最终检索总 session 数最高的冠军对象。我习惯于总是对值/路径进行硬编码,因为它的大小总是静态设置的,而这里的大小可以改变。
我相信我需要做以下事情......但我不完全确定
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。
最佳答案
>
获得最高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>
关于javascript - 跟踪具有动态(变化的)数组大小的 JSON 字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27273208/
有没有办法在 xdebug 跟踪输出中查看 echo 或 print 函数调用。我正在为我在我的服务器中运行的所有脚本寻找一个全局配置(或一种方法)。 例子: 我希望跟踪输出显示 echo 调用。默
我将应用程序从2.0.0M2升级到了2.1.0,但是当我尝试运行该应用程序时,出现此错误: Note: /Volumes/Info/proyectos-grails/vincoorbis/Member
我如何在共享点中执行日志记录。我想使用跟踪。 以便它记录 12 个配置单元日志。 最佳答案 微软提供了一个例子: http://msdn.microsoft.com/en-us/library/aa9
如何跟踪 eclipse 和 android 模拟器的输出。我习惯于在 Flash 和 actionscript 中这样做。 在 AS3 中它将是: trace('我的跟踪语句'); 最佳答案 您有几
是否可以在 Postgresql 上进行查询跟踪?我在带有 OLEDB 界面的 Windows 上使用 9.0。 此外,我需要它是实时的,而不是像默认情况下那样缓冲... 最佳答案 我假设您的意思是在
第一天 HaxeFlixel 编码器。愚蠢的错误,但谷歌没有帮助我。 如何使用 Haxe、NME 和 Flixel 追踪到 FlashDevelop 输出。它在使用 C++ 执行时有效,但对 Flas
我有一个关于 iPhone 上跟踪触摸的快速问题,我似乎无法就此得出结论,因此非常感谢任何建议/想法: 我希望能够跟踪和识别 iPhone 上的触摸,即。基本上每次触摸都有一个起始位置和当前/移动位置
我正在做我的大学项目,我只想跟踪错误及其信息。错误信息应该与用户源设备信息一起存储在数据库中(为了检测源设备,我正在使用MobileDetect扩展名)。我只想知道应该在哪里编写代码,以便获得所有错误
我正在 Azure 中使用多个资源,流程如下所示: 从 sftp 获取文件 使用 http 调用的数据丰富文件 将消息放入队列 处理消息 调用一些外部电话 传递数据 我们如何跟踪上述过程中特定“运行”
在我的 WCF 服务中,当尝试传输大数据时,我不断收到错误:底层连接已关闭:连接意外关闭 我想知道引发此错误的具体原因,因此我设置了 WCF 跟踪并可以读取 traces.svclog 文件。 问题是
我的目标是在 Firebase Analytics 中获取应用数据,在 Google Universal Analytics 中获取其他自定义数据和应用数据。 我的问题是我是否在我的应用上安装 Fir
我正在 Azure 中使用多个资源,流程如下所示: 从 sftp 获取文件 使用 http 调用的数据丰富文件 将消息放入队列 处理消息 调用一些外部电话 传递数据 我们如何跟踪上述过程中特定“运行”
我们正在考虑跟踪用户通过 Tridion 管理的网站的旅程的要求,然后能够根据此行为将此用户识别为“潜在客户”,然后如果他们在之后没有返回,则触发向此用户发送电子邮件X 天。 SmartTarget
在 Common Lisp 中,函数(跟踪名称)可用于查看有关函数调用的输出。 如果我的函数是用局部作用域声明的,我如何描述它以进行跟踪? 例如,如何跟踪栏,如下: (defun foo (x)
有什么方法可以检测文本框的值是否已更改,是用户明确更改还是某些 java 脚本代码修改了文本框?我需要检测这种变化。 最佳答案 要跟踪用户更改,您可以添加按键处理程序: $(selector).key
int Enable ( int pid) { int status; #if 1 { printf ( "child pid = %d \n", pid ); long ret =
关闭。这个问题需要多问focused 。目前不接受答案。 想要改进此问题吗?更新问题,使其仅关注一个问题 editing this post . 已关闭 9 年前。 Improve this ques
我有以下测试代码: #include int main(void) { fprintf(stderr, "This is a test.\n"); int ret = open("s
我有一个闭源 Java 应用程序,供应商已为其提供了用于自定义的 API。由于我没有其他文档,我完全依赖 API 的 javadoc。 我想跟踪特定用例在不同类中实际调用的方法。有什么办法可以用 ec
我正在学习 PHP。我在我的一个 php 函数中使用了如下所示的 for 循环。 $numbers = $data["data"]; for ($i = 0;$i send($numbers[
我是一名优秀的程序员,十分优秀!