Use Array reduce()
var result = res.reduce((accu, obj) => {
accu[obj.url] = (accu[obj.url] || 0) + obj.views;
return accu;
},
{}
);
Output from node CLI:
> var result = res.reduce((accu, obj) => { accu[obj.url] = (accu[obj.url] || 0) + obj.views; return accu; }, {});
undefined
> result
{ '/page1': 16, '/page2': 5, '/page3': 29 }
solved How to group and add their value in array of objects in javascript?