如何使用JavaScript或jQuery在多维JSON对象中找到特定节点的最大值?
How can I find the highest value of a particular node within a multidimensional JSON object with JavaScript or jQuery
下面是我正在使用的对象的简短示例。
{
"myservices": [
{
"name": "oozie",
"hostidn": "1",
"details": "failed process health monitor....",
"currstatus": "Warning",
"currstatusclass": "warning"
},
{
"name": "oozie",
"hostidn": "2",
"details": "failed process health monitor....",
"currstatus": "Warning",
"currstatusclass": "warning"
},
{
"name": "oozie",
"hostidn": "3",
"details": "failed process health monitor....",
"currstatus": "Warning",
"currstatusclass": "warning"
},
{
"name": "oozie",
"hostidn": "4",
"details": "failed process health monitor....",
"currstatus": "Warning",
"currstatusclass": "warning"
},
{
"name": "oozie",
"hostidn": "5",
"details": "failed process health monitor....",
"currstatus": "Warning",
"currstatusclass": "warning"
},
{
"name": "single-namenode",
"hostidn": "2",
"details": "failed process health monitor....",
"currstatus": "Warning",
"currstatusclass": "warning"
}
]
}
我最终想要在运行所有这些并显示它们之前找到最高的"hostidn"是什么。hostidn是第n个数字,它可以是唯一的数字,也可以是数百个深度,中间有几个重复的数字。我的目标是找到最高的一个,并在此基础上对其进行for或while循环,将它们组合在一起,在视觉显示中。请注意,我在下面有一个hostidn,数字是2,而其余的都有自己的。我想组两个2一起在一个框显示,但有5个不同的hostidn在这种情况下。我不知道,也许我想错了,不过我会接受建议的。
基本算法可以遵循
声明并设置变量为0,如
$ currentHighest = 0;
然后遍历json数组,在每次迭代中比较hostidn
与$currentHighest
的值,如果该值高于$currentHighest
中已经存在的值,则将该值设置为$currentHighest
$currentHighest=0;
$(data.myservices).each(function(index, element){
if(data.myservices[index].hostidn>$currentHighest)
$currentHighest=data.myservices[index].hostidn;
});
//loop ends and `$currentHighest` will have the highest value
在迭代结束时,您将获得$currentHighest
试过了
$(function(){
$.post("highest.json",function(data){
$currentHighest=0;
$(data.myservices).each(function(index, element){
if(data.myservices[index].hostidn>$currentHighest)
$currentHighest=data.myservices[index].hostidn;
});
alert($currentHighest);
},'json');
});
返回包含一个或多个具有最高idn的对象的数组。也请参阅http://jsfiddle.net/Kai/DUTK7/(日志到控制台)
function getHighHostIdn (json) {
var i = 0;
hostidns = [],
len = json.myservices.length,
highest = null,
matches = [];
for (; i < len; i++) {
hostidns.push(parseInt(json.myservices[i].hostidn, 10));
}
highest = Math.max.apply(null, hostidns);
for (i = 0, len = hostidns.length; i < len; i++) {
if (hostidns[i] === highest) {
matches.push(json.myservices[i]);
}
}
return matches;
}
我喜欢使用linq.js来做这些事情,它允许你在代码中避免许多传统的for循环(可见)。当然,您可以为每个用例编写更优化的代码,但对于大多数情况,性能不是那么重要,更简洁/更短的代码是更大的好处。
如果只可能有一个最大值,或者如果你不在乎你得到的是哪一个,只要它有最大值,然后做这样的事情:
var result = Enumerable.From(obj.myservices).MaxBy('$.hostidn');
或者如果你想要多个max对象,那么这样做:
var e = Enumerable.From(obj.myservices);
var result2 = e.Where('$.hostidn == ' + e.Max('$.hostidn')).ToArray();
您可以在这里看到实际的代码:http://jsfiddle.net/sTaHv/13/编辑:我还添加了一个排序的例子,因为我看到你提到了。
有关linq.js的更多信息,请访问项目页面:http://linqjs.codeplex.com/
- 在Javascript数组中查找绝对最大值
- Html5输入属性的默认值,如最小值、最大值、大小等
- 按最大值获取数组索引
- 识别滑块范围的最小值和最大值
- 如何在最大值之前的某个点停止范围滑块
- 访问Highcharts系列的最小/最大值:afterSetExtremes不会激发
- 如何在拖动后获得图表的最小值和最大值放大高图表
- AngularJs:在数字输入字段中设置验证的条件最小值和最大值
- HTML5范围滑块最小值和最大值
- 如何仅在存在最小值和最大值时才显示错误消息
- 在 JavaScript 事件中添加最小值和最大值
- 从 JavaScript 中的数组中获取最大值和最小值
- 获取整数对象属性名称的最小值和最大值
- 高图表 - 在缩放重置中设置最大值和最小值
- Java 脚本 设置日期的最小值和最大值属性
- 使用javascript动态更改select中的最大值
- 如何设置jQuery微调器的最大值和最小值
- 当达到最小值和最大值时,防止在输入中显示额外的文本
- 使用函数从数组中查找最小值和最大值
- 如何使用JavaScript或jQuery在多维JSON对象中找到特定节点的最大值?