【问题标题】:How can I find the highest value of a particular node within a multidimensional JSON object with JavaScript or jQuery如何使用 JavaScript 或 jQuery 在多维 JSON 对象中找到特定节点的最大值
【发布时间】:2011-12-04 20:16:23
【问题描述】:

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

{
    "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"
        }
    ]
}

我最终想在运行所有这些并显示它们之前找到最高的“hostid”是什么。 hostidn 是第 N 个数字,它可以是唯一的数字,也可以是数百个深度,中间有多个重复。我的目标是找到最高的一个,并在此基础上对其执行 for 或 while 循环,以在可视化显示中将它们组合在一起。示例通知我在下面有一个带有数字 2 的 hostidn,而其余的都有自己的。我想将两者与 2 组合在一个框中以进行显示,但在这种情况下有 5 个不同的 hostidn。我不知道也许我想错了,不过我会接受建议。

【问题讨论】:

  • 是你想显示所有具有最高hostid的问题,包括欺骗?
  • 你想得到最高的数字还是出现次数最多的数字?无论如何,您必须遍历数组并比较数字,那么您的实际问题是什么?
  • 嗯,基本上我想显示所有的,包括基于 hostidn 的欺骗。在这种情况下,hostid 将充当一个容器,然后将在该容器中显示每个具有相同 hostidn 的容器。这里的问题是 hostidn 可以是 1 到 200+ 之间的任何值,所以我没有设置的数字可以使用。另一个问题是它们在对象中输出的方式是说随机性最小所以我必须找到最高的数字,这样我才能运行一段时间,比如基于从低到高的循环,这些数字将永远是为了我的知识意味着没有计数的差距。我只是不知道高数是多少

标签: javascript jquery json sorting object


【解决方案1】:

我喜欢将 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/

【讨论】:

    【解决方案2】:

    你可以遵循的基本算法

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

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

    【讨论】:

      【解决方案3】:

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

      【讨论】:

        猜你喜欢
        • 2018-08-10
        • 2019-01-25
        • 1970-01-01
        • 1970-01-01
        • 2011-02-07
        • 1970-01-01
        • 2017-06-19
        • 1970-01-01
        相关资源
        最近更新 更多