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

3
这是我正在使用的对象的简短示例。
{
    "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”,它是第N个数字,可以是唯一数字,也可以有数百个重复项。我的目标是找到最高的一个,并基于它进行for或while循环,以在视觉显示中将它们分组在一起。示例:注意下面有一个带有数字2的hostidn,而其余所有数字都有自己的数字。我想将具有2个数字的两个组合在一起,以便在显示框中显示,但在这种情况下有5个不同的hostidn。也许我想错了,我会接受建议。


你想显示所有具有最高hostidn的记录,包括重复项吗? - Evan
你想要获取最大的数字还是出现次数最多的数字?无论哪种情况,你都需要遍历数组并比较数字,那么你实际上的问题是什么? - Felix Kling
基本上,我想根据hostidn显示所有包括重复项的内容。在这种情况下,hostidn将充当容器,然后在该容器中显示具有相同hostidn的每个内容。问题在于,hostidn可以是从1到200+的任何内容,因此我没有固定的数字可供使用。另一个问题是它们在对象中的输出方式非常随机,所以我必须找到最高的数字,以便我可以运行while循环,基于从低到高的数字始终按顺序排列,据我所知,计数中没有间隙。我只是不知道高位数字是多少。 - donkeylips
3个回答

1

基本算法可以遵循

声明并将变量设置为零,如下所示

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

0

返回一个包含一个或多个具有最高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;
}

0

我喜欢使用linq.js来处理这些事情,它可以让你在代码中避免很多传统的for循环(显而易见)。当然,你可能可以为每个用例编写更优化的代码,但对于大多数事情来说,性能并不那么重要,更清晰/更短的代码是更大的好处。

如果只有一个最大值的机会,或者如果你不关心你得到哪一个,只要它是具有最大值的一个,那么就像这样做:

var result = Enumerable.From(obj.myservices).MaxBy('$.hostidn');

或者,如果您可以拥有并且想要多个最大对象,则可以这样做:

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/


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接