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

{
    "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和2组合在一个框中以进行显示,但是在这种情况下有5个不同的hostidn。我不知道,也许我在想错,但我会提出建议。

最佳答案

您可以遵循的基本算法

声明变量并将其设置为零,例如

$ 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');
});

09-04 20:34