-1

If I have an Array with years Y=[2014, 2011, 2010, 2013, 2014, 2007, 2004], and if I want to group these years for n and make object like this (n=3 lets say):

property name is min(Y) - (min(Y)+3), property value: average like (min(Y) + (min(Y)+3))/2).

obj={"2004-2007": "2005.5", "2008-2011":"2009.5", "2012-2015": "2013.5"}

Further, I would later like to be able to get my average value, so if I ask for 2009 i get the value of group in which this element belong, or 2009.5.

Is it possible? Probably there is a better way? I don't care how object looks, I would just like to get my group average value when I ask for it.

3
  • 2104 is quite far from the others, it will screw up the average. Commented May 27, 2015 at 15:11
  • Now 2014 is in there twice Commented May 27, 2015 at 15:31
  • yeah, my array Y have some years which are repeated, some of them many times. Commented May 27, 2015 at 15:39

1 Answer 1

1

Assume the grouping is based on continuous year instead of every 4 of the year in data list, i.e even there is no data for year 2016-2019, there is still a group for it.

function GroupedAvgData(dataList, groupSize){
    //get the min(Y) for a given year
    function getMinY(year, minYear){
        var gap = year - minYear;
        return minYear + gap - gap%groupSize;
    }

    //build a groups with minY as key and average as value
    function buildGrouping(sortedDataList, groupSize){
        var currentMin=0, currentSum=0, currentCount=0, groups={};
        for(var i=0,len=sortedDataList.length; i<len;i++){
            var minY = getMinY(sortedDataList[i], sortedDataList[0]);
            if(currentMin != minY){
                if(currentCount > 0){
                    groups[currentMin] = currentSum / currentCount;
                }
                currentCount = 1;
                currentMin = minY;
                currentSum = sortedDataList[i];
            }else{
                currentSum += sortedDataList[i];
                currentCount++;
            }
            if( i == len - 1 && currentCount>0){
                groups[currentMin] = currentSum / currentCount;
            }
        }
        return groups;
    }
    this.sortedList = dataList.sort(function(a,b){return a - b;});
    this.groups = buildGrouping(this.sortedList, groupSize);
    this.groupSize = groupSize; 
}

GroupedAvgData.prototype.getAverageForYear = function(year){
    var gap = year - this.sortedList[0];
    if(gap >= 0){
        var minY = this.sortedList[0] + (gap-gap%this.groupSize);
        if(this.groups[minY]){return this.groups[minY];}
    }
    return 0;
}

To use it, i.e. query the average for some year:

var Y=[2014, 2011, 2010, 2013, 2014, 2007, 2004];
var myAvg = new GroupedAvgData(Y, 4);
console.log(myAvg.getAverageForYear(2009));
Sign up to request clarification or add additional context in comments.

1 Comment

your comment helped, but this is even better. tnx

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.