我现在真的被困在这里2天了。我有一个形式的JS对象:
{
"sqlValuePair":[
{
"repLag":{
"host":"abc.com",
"metric":"Replication_Lag",
"value":{
"24-09-2014 13:20_0":0
},
"tags":"",
"alert":"OKAY"
},
"slowQuery":{
"host":"abc.com",
"metric":"Slow_Queries",
"value":{
"24-09-2014 13:20_2":220
},
"tags":"",
"alert":"ALERT"
},
"status":{
"host":"abc.com",
"metric":"Status",
"value":{
"24-09-2014 13:20_0":1
},
"tags":"",
"alert":"OKAY"
}
},
{
"repLag":{
"host":"abc.com",
"metric":"Replication_Lag",
"value":{
"24-09-2014 13:15_0":0
},
"tags":"",
"alert":"OKAY"
},
"slowQuery":{
"host":"abc.com",
"metric":"Slow_Queries",
"value":{
"24-09-2014 13:15_0":2
},
"tags":"",
"alert":"OKAY"
},
"status":{
"host":"abc.com",
"metric":"Status",
"value":{
"24-09-2014 13:15_0":1
},
"tags":"",
"alert":"OKAY"
}
}
]
}
我需要做的是制作一张看起来像这样的表:
**Health metric date interval 1 date interval 2**
abc.com replication Lag OKAY ALERT
abc.com status OKAY ALERT
abc.com Slow queries OKAY OKAY
def.com replication Lag OKAY ALERT
def.com status OKAY ALERT
def.com Slow queries OKAY OKAY
但是要动态填充复制滞后条目,我需要遍历对象,查找host = abc.com,然后查找metric =复制滞后,然后填写该日期间隔的条目。 有没有比以下更有效的方式:
var obj = JSON.parse(json.replace(/"/g, '"'));
console.log("the main keys are "+Object.keys(obj));
var timeList = obj.timeList;
var sqlValuePair = obj.sqlValuePair;
for(i=0;i<timeList.length;i++){
$("#table").append('<th>'+Object.keys(timeList[i])+'</th>');
}
for(i=0;i<Object.keys(obj).length;i++){
var channel = Object.keys(obj)[i];
console.log(channel); // SQL in this case
var list = sort(sqlValuePair); // this is giving an ERROR.
console.log(" list is "+list);
}
function comparator(Obj1, Obj2){
if(obj1.host.localeCompare(obj.host)>=0)
return 1;
else return 0;
}
function sort(list){
var temp;
console(" entered sort ");
console.log("the list length is "+list.length);
for(var i=1;i<list.length;i++){
for(var j=0;j<=i-1;j++){
if(comparator(list[j],list[i])>0)
{
temp=list[i];
list[i]=list[j];
list[j]=temp;
}
}
}
console.log(" the sorted list is "+list);
return list
}