如何遍历对象数组

时间:2015-12-10 13:54:21

标签: javascript arrays dygraphs

我有这个对象数组:

[{
    "AVG_VALUE": "38",
    "MAX_VALUE": "38",
    "SUM_VALUE": "38",
    "MIN_VALUE": "38",
    "METRICID": "100367597885",
    "START": "1449216120000",
    "STARTTIME": "09:02"
}, {
    "AVG_VALUE": "0",
    "MAX_VALUE": "0",
    "SUM_VALUE": "0",
    "MIN_VALUE": "0",
    "METRICID": "100367597885",
    "START": "1449481620000",
    "STARTTIME": "10:47"
}, {
    "AVG_VALUE": "0",
    "MAX_VALUE": "0",
    "SUM_VALUE": "0",
    "MIN_VALUE": "0",
    "METRICID": "100367597879",
    "START": "1449506820000",
    "STARTTIME": "17:47"
}, {
    "AVG_VALUE": "0",
    "MAX_VALUE": "0",
    "SUM_VALUE": "0",
    "MIN_VALUE": "0",
    "METRICID": "100367597886",
    "START": "1449506820000",
    "STARTTIME": "17:47"
}, {
    "AVG_VALUE": "1553",
    "MAX_VALUE": "1553",
    "SUM_VALUE": "1553",
    "MIN_VALUE": "1553",
    "METRICID": "100367597885",
    "START": "1449578820000",
    "STARTTIME": "13:47"
}, {
    "AVG_VALUE": "1543",
    "MAX_VALUE": "1543",
    "SUM_VALUE": "1543",
    "MIN_VALUE": "1543",
    "METRICID": "100367597879",
    "START": "1449579120000",
    "STARTTIME": "13:52"
}, {
    "AVG_VALUE": "1553",
    "MAX_VALUE": "1553",
    "SUM_VALUE": "1553",
    "MIN_VALUE": "1553",
    "METRICID": "100367597886",
    "START": "1449579120000",
    "STARTTIME": "13:52"
}, {
    "AVG_VALUE": "514.3333",
    "MAX_VALUE": "1543",
    "SUM_VALUE": "1543",
    "MIN_VALUE": "0",
    "METRICID": "100367597879",
    "START": "1449652080000",
    "STARTTIME": "10:08"
}, {
    "AVG_VALUE": "10",
    "MAX_VALUE": "10",
    "SUM_VALUE": "30",
    "MIN_VALUE": "10",
    "METRICID": "100367597886",
    "START": "1449652080000",
    "STARTTIME": "10:08"
}, {
    "AVG_VALUE": "30",
    "MAX_VALUE": "30",
    "SUM_VALUE": "30",
    "MIN_VALUE": "30",
    "METRICID": "100367597879",
    "START": "1449705900000",
    "STARTTIME": "01:05"
}, {
    "AVG_VALUE": "30",
    "MAX_VALUE": "30",
    "SUM_VALUE": "30",
    "MIN_VALUE": "30",
    "METRICID": "100367597886",
    "START": "1449705900000",
    "STARTTIME": "01:05"
}]

对于我的图表,我需要" AVG_VALUE"和这样的字符串:

"Date, ANZAHL_FAKTUREN_GESAMT, ANZAHL_VERARB_DATEIEN, VERSENDETE_SPOOL
\n2015/12/04, 0, 38, 0
\n2015/12/07, 0, 0, 0
\n2015/12/08, 1543, 1553, 1553
\n2015/12/09, 514.3333, 0, 10
\n2015/12/10, 30, 0, 30"

所以我用这段代码迭代我的数组:

var j = 0;
    for (var i = 0; i < (data.length + j); i++) {
        if (i % metricNames.length === 0) {
            var date = new Date(null);
            date.setMilliseconds(data[i]['START']);
            body += "\n" + date.customFormat("#YYYY#/#MM#/#DD#");

        }

        console.log("i-j " + (i - j));
        console.log("modulo " + i % metricNames.length);
        console.log("m1 " + metricNames[i % metricNames.length]['METRICID']);
        console.log("m2 " + data[i - j]['METRICID']);

        if (metricNames[i % metricNames.length]['METRICID'] === data[i - j]['METRICID']) {
            console.log(i % metricNames.length);
            body += ", " + data[i - j]['AVG_VALUE'];
        } else { // if there is no value for the metricid no value will be added
            body += ", 0";
            j++;
        }
    }

这适用于其他数据集,但问题是,在示例中度量值的位置正在发生变化。 [1]是[2]创建的......但应该是相反的。所以最后我的结果将是:

Date, ANZAHL_FAKTUREN_GESAMT, ANZAHL_VERARB_DATEIEN, VERSENDETE_SPOOL
2015/12/04, 0, 38, 0
2015/12/07, 0, 0, 0
2015/12/07, 0, 0, 0
2015/12/08, 0, 1553, 0
2015/12/08, 1543, 0, 1553
2015/12/09, 514.3333, 0, 10
2015/12/10, 30, 0, 30

是否有任何建议如何遍历数据集以获取正确的字符串?

//*** This code is copyright 2002-2003 by Gavin Kistner, !@phrogz.net
//*** It is covered under the license viewable at http://phrogz.net/JS/_ReuseLicense.txt
Date.prototype.customFormat = function(formatString){
  var YYYY,YY,MMMM,MMM,MM,M,DDDD,DDD,DD,D,hhhh,hhh,hh,h,mm,m,ss,s,ampm,AMPM,dMod,th;
  YY = ((YYYY=this.getFullYear())+"").slice(-2);
  MM = (M=this.getMonth()+1)<10?('0'+M):M;
  MMM = (MMMM=["January","February","March","April","May","June","July","August","September","October","November","December"][M-1]).substring(0,3);
  DD = (D=this.getDate())<10?('0'+D):D;
  DDD = (DDDD=["Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"][this.getDay()]).substring(0,3);
  th=(D>=10&&D<=20)?'th':((dMod=D%10)==1)?'st':(dMod==2)?'nd':(dMod==3)?'rd':'th';
  formatString = formatString.replace("#YYYY#",YYYY).replace("#YY#",YY).replace("#MMMM#",MMMM).replace("#MMM#",MMM).replace("#MM#",MM).replace("#M#",M).replace("#DDDD#",DDDD).replace("#DDD#",DDD).replace("#DD#",DD).replace("#D#",D).replace("#th#",th);
  h=(hhh=this.getHours());
  if (h==0) h=24;
  if (h>12) h-=12;
  hh = h<10?('0'+h):h;
  hhhh = h<10?('0'+hhh):hhh;
  AMPM=(ampm=hhh<12?'am':'pm').toUpperCase();
  mm=(m=this.getMinutes())<10?('0'+m):m;
  ss=(s=this.getSeconds())<10?('0'+s):s;
  return formatString.replace("#hhhh#",hhhh).replace("#hhh#",hhh).replace("#hh#",hh).replace("#h#",h).replace("#mm#",mm).replace("#m#",m).replace("#ss#",ss).replace("#s#",s).replace("#ampm#",ampm).replace("#AMPM#",AMPM);
};

var metricNames = null;

for (var i = 0; i < metricNames.length; i++) {
    header += ", " + metricNames[i]['METRICNAME'];
}

1 个答案:

答案 0 :(得分:0)

你的问题是对象属性的顺序,对吧?

有时:

{
    "AVG_VALUE" : "0", 
    "MAX_VALUE" : "0",
    "SUM_VALUE" : "0",
    "MIN_VALUE" : "0",
    "METRICID" : "100367597885",
    "START" : "1449481620000",
    "STARTTIME" : "10:47"
}

以及后来的:

{
    "START" : "1449481620000",
    "AVG_VALUE" : "0", 
    "MIN_VALUE" : "0",
    "METRICID" : "100367597885",
    "MAX_VALUE" : "0",
    "STARTTIME" : "10:47",
    "SUM_VALUE" : "0",
}

正确?如果这是你的问题,那么试试

for(var i = 0; i<data.length; i++){
    var line = new Array();
    for(var key in data[i]){
        var pos;
        switch(key){
            case "STARTTIME": pos = 2 break;
            case "SUM_VALUE": pos = 5 break;
            ...
        }
        line[pos] = data[i][key];
    }
    line = line.join(',');
}

如果需要进一步解释,我会通过评论来做到这一点。如果我的问题出错了,我很抱歉:)