Here is an example of an object array:
[{
"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"
}]
I am looking to extract the "AVG_VALUE" from this array and create a specific formatted string:
"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"
To achieve this, I will iterate through my array using the following code:
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++;
}
}
While this code works with other datasets, in this particular case, the order of the metric values are not consistent causing incorrect results as shown below:
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
Any suggestions on how to improve the iteration process to get the correct string output?
//*** This code is copyright 2002-2003 by Gavin Kistner, <a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="260766564e5449415c08484352">[email protected]</a>
//*** 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'];
}