如何使用JavaScript或jQuery在多维JSON对象中找到特定节点的最大值?

How can I find the highest value of a particular node within a multidimensional JSON object with JavaScript or jQuery

本文关键字:节点 最大值 对象 JavaScript 何使用 jQuery JSON      更新时间:2023-09-26

下面是我正在使用的对象的简短示例。

{
    "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/