I have an example array. It contains a day and a number of failures of builds.
var array = [["2014-08-13",3],
["2014-08-13",3],
["2014-08-14",4],
["2014-08-12",2],
["2014-08-13",3],
["2014-08-12",2]];
I want to iterate through the array and get an array which holds for each day the average value of number of failures. I tried out some things but couldn't find a proper solution.
The target array should look like this:
var targetArray = [["2014-08-13",3],
["2014-08-14",4],
["2014-08-12",2]];
what I got so far is to make an array which holds the three dates:
Array.prototype.contains = function(obj) {
var i = this.length;
while (i--) {
if (this[i] == obj) {
return true;
}
}
return false;
}
var array = [
["2014-08-13", 3],
["2014-08-13", 3],
["2014-08-14", 4],
["2014-08-12", 2],
["2014-08-13", 3],
["2014-08-12", 2]];
var targetArray = [];
for (var i = 0; i < array.length; i++) {
var temporaryArr = [];
var current = array[i];
var currentDate = current[0];
var currentValue = current[1];
console.log("current: " + current);
if (!targetArray.contains(currentDate)) {
temporaryArr[0] = currentDate;
targetArray[targetArray.length] = temporaryArr;
}
}
console.log(targetArray);