1

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!

1
  • 1
    look at the reduce function, awesome for this use case. Commented Aug 20, 2014 at 19:00

1 Answer 1

1

You can use reduce with an initial accumulator:

function weekRange(begin, end, data) {
  return data.filter(function(x) {
    return x.WeekNo >= begin && x.WeekNo <= end;
  }).reduce(function(acc, x) {
    Object.keys(acc).forEach(function(k) {
      acc[k] += Number(x[k]);
    });
    return acc;
  },{
    displayAd_imp: 0,
    videoAd_imp: 0,
    tv_imp: 0
  });
}
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks for the quick response! The above snippet is returning NaN - "{displayAd_imp: NaN, videoAd_imp: NaN, tv_imp: NaN}". Perhaps I may be doing something wrong. I made a jsfiddle in the above post, please feel free to use that to project.
That's interesting, I see why the numbers were not working for me. the properties had a different format. I have updated jsfiddle.net/64gcjLku/9. The Number(x[k]) can't read the value since it contains ",". I will try to look into this.
Updated jsfiddle.net/64gcjLku/10. It should now support the above format as well. THANKS EVERYONE!!

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.