2011-10-10 15 views
3

以下是我正在使用的對象的簡短示例。如何使用JavaScript或jQuery查找多維JSON對象中特定節點的最高值

{ 
    "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,其中所有其他的都有自己的號碼。我想將這兩個在一起放在一個盒子中用於顯示,但在這種情況下有5個不同的hostidn。我不知道,也許我正在想它錯了,我會採取建議,但是。

+0

是要顯示所有具有最高hostidn的那些,包括易受騙的人的問題? – Evan

+0

你想得到最高的數字還是發生得最多的數字?無論如何,你必須迭代數組並比較數字,那麼你的實際問題是什麼? –

+0

迭代........... – Rafay

回答

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對於這類事情,它可以讓你避免很多傳統代碼中的(明顯)循環。當然,你可以爲每個用例編寫更優化的代碼,但是對於大多數情況來說,性能並不那麼重要,更簡潔/更短的代碼是更大的好處。

如果只有一個最大值機會,或者如果你不關心你是哪一個,只要其具有最大值的一個,然後做這樣的事情:

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/

相關問題