I would like to aggregate properties in an array of objects and produce another object with totals.
Here is an example of what I'm working on:
Below is an array of the objects, and we need to sum displayAd_imp, videoAd_imp, tv_impbased on WeekNo range.
var objArr = [{
"Title": "July 13 - July 19 2014",
"displayAd_imp": "15",
"videoAd_imp": "75",
"tv_imp": "120",
"Date": "2014-07-17T00:00:00.000Z",
"WeekNo": 29
}, {
"Title": "July 20 - July 26 2014",
"displayAd_imp": "25",
"videoAd_imp": "65",
"tv_imp": "130",
"Date": "2014-07-24T00:00:00.000Z",
"WeekNo": 30
}, {
"Title": "July 27 - Aug 2 2014",
"displayAd_imp": "35",
"videoAd_imp": "55",
"tv_imp": "140",
"Date": "2014-07-31T00:00:00.000Z",
"WeekNo": 31
}, {
"Title": "Aug 3 - Aug 9 2014",
"displayAd_imp": "55",
"videoAd_imp": "25",
"tv_imp": "20",
"Date": "2014-08-07T00:00:00.000Z",
"WeekNo": 32
}, {
"Title": "Aug 10 - Aug 17 2014",
"displayAd_imp": "10",
"videoAd_imp": "70",
"tv_imp": "120",
"Date": "2014-08-14T00:00:00.000Z",
"WeekNo": 33
}]
The function below gets the range. We pass data, start-week, and end-week as params and it filters the data by desired range. I have a JSfiddle example to show the output.
function CalcWeekRange(data,begin,end){
console.log(data,begin, end);
newArr = data.filter(function(item){
return (item.WeekNo >= begin && item.WeekNo <= end);
});
console.log(newArr);
}
After this I need to sum up the result into an object that would look something like:
{
"displayAd_imp": "140",
"videoAd_imp": "290",
"tv_imp": "530",
}
The last part is where I'm stuck :(
Hope I made the question clear, thanks in advance!