开发者

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

开发者 https://www.devze.com 2023-04-12 06:07 出处:网络
Here is a short sample of the object I am working with. { \"myservices\": [ { \"name\": \"oozie\", \"hostidn\": \"1\",

Here is a short sample of the object I am working with.

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

I ultimately want to find what the highest "hostidn" is before running through all these and开发者_JAVA百科 displaying them. hostidn is a Nth number it can be the only number or it can be hundreds deep with several duplicates in between. My Goal is to find that highest one and do a for or while loop on it based on that to group them together in a visual display. Example notice I have one hostidn below with the number 2 while all the rest have there own. I would want to group the two with 2 together in a box for display but there is 5 different hostidn in this scenario. I dunno maybe I am thinking it over wrong I'll take suggestions however.


basic algo you can follow

declare and set a variable to zero like

$currentHighest=0;

then iterate over the json array and on each iteration campare the value of hostidn with the $currentHighest if the value is higher than the value already existing in the $currentHighest set that value to $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

at the end of the iteration you will get the highest value in the $currentHighest

Tried and tested

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


Returns an array containing one or more objects with the highest idn. Also see http://jsfiddle.net/Kai/DUTK7/ (logs to console)

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;
}


I like to use linq.js for these kind of things, it allows you to avoid many traditional for loops in your code (visibly). Sure you can probably write more optimized code for each use case, but for most things performance is not so important and the cleaner/shorter code is a greater benefit.

If there is only chance for one max value, or if you don't care which one you get, as long as its the one that have the max value, then do something like this:

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

Or if you can have and want multiple max objects, then do this:

var e = Enumerable.From(obj.myservices);
var result2 = e.Where('$.hostidn == ' + e.Max('$.hostidn')).ToArray();

You can see the code in action here: http://jsfiddle.net/sTaHv/13/ EDIT: I also added a sorting example since I saw that you mentioned that.

To read more about linq.js go to the project page: http://linqjs.codeplex.com/

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号