[英]javascript/jquery find id number within multidimensional json object and use the object that id number is in as an object by itself
[英]How can I find the highest value of a particular node within a multidimensional JSON object with JavaScript or jQuery
這是我正在使用的對象的簡短示例。
{
"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,而其余的都有自己的。 我想把兩個一起組合在一個盒子里展示,但在這個場景中有5個不同的hostidn。 我不知道也許我認為它錯了我會接受建議。
你可以遵循的基本算法
聲明並將變量設置為零
$ currentHighest = 0;
然后迭代json數組,並在每次迭代時使用$currentHighest
的hostidn
值,如果該值高於$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');
});
返回包含一個或多個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;
}
我喜歡使用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/
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.