0

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);
0

3 Answers 3

1

First, your dates need to be strings or you'll start getting some really weird output:

var arr = [
  ['2014-08-13', 3],
  ['2014-08-13', 3],
  ['2014-08-14', 4],
  ['2014-08-12', 2],
  ['2014-08-13', 3],
  ['2014-08-12', 2]
];

Create a new object. We're going to use its keys to store our dates and values to store the failures and number of times that day has appeared.

var obj = {};

Loop over the array. If the key/date doesn't exist add it to the object and set the value to an array containing the number of failures and set the current count to 1. If the key/date does exist, add the number of fails to the fail total and increment the count.

for (var i = 0, l = arr.length; i < l; i++) {
  var el = arr[i];
  var date = el[0];
  var fails = el[1];
  if (!obj[date]) {
    obj[date] = [fails, 1]
  } else {
    obj[date] = [obj[date][0] + fails, obj[date][1] + 1]
  }
}

Finally loop over the object and push the date and average back to a new array.

var out = [];
for (var date in obj) {
  out.push([date, obj[date][0] / obj[date][1]])
}

DEMO

Sign up to request clarification or add additional context in comments.

5 Comments

how can this behaviour can be modified so that is has 2 places for values like [[2014-08-13,3,13],[2014-08-13,3,13],[2014-08-14,4,14]..] and returns an array like [[2014-08-13,3,32,5],..]?
@sanyooh, what is that second value?
this would be like for each day number of failures and successes for each day as an average value.
You can do that but the successes would have to be represented in the original data, wouldn't it? You can't just make up the figures.
More like this. You only need the count represented once in the array that you're building.
1

Code below:

var array = [
    ["2014-08-13", 3],
    ["2014-08-14", 4],
    ["2014-08-13", 3],
    ["2014-08-12", 2],
    ["2014-08-13", 3],
    ["2014-08-12", 2]
];

function average(array) {
    var ret = {}, retArr = [], tmp, time, value;
    for(var i=0, len=array.length; i < len; i++) {
        tmp = array[i];
        time = tmp[0];
        value = tmp[1];
        if(time in ret) {
            ret[time].push(value)
        } else {
            ret[time] = [value];
        }
    }
    for(var p in ret) {
        var total = 0;
        ret[p].forEach(function(val) {
            total += val;
        });
        retArr.push([p, total / ret[p].length]);
    }
    return retArr;
}

average(array);

Comments

0
var myArray = [["2014-08-13", 3],
              ["2014-08-13", 3],
              ["2014-08-14", 4],
              ["2014-08-12", 2],
              ["2014-08-13", 3],
              ["2014-08-12", 2]];

function getAverage(arr) {
  var dates = [];
  var values = [];

  for(i=0; i<arr.length; i++){
    var index = dates.indexOf(arr[i][0]);     
    if(index == -1){
       dates.push(arr[i][0]);
       values.push([arr[i][1]]);
    }else{
       values[index].push(arr[i][1]);
    }
  }

  for(d=0; d<dates.length;d++){
    var dateTotal = 0;
    for(a=0;a<values[d].length;a++){ dateTotal += values[d][a] }
    dates[d] = [dates[d], (dateTotal / values[d].length)]
  }

  return dates
};

getAverage(myArray);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.