JSFiddle - React, Tailwind, and code Playground
JavaScript
var array = [{
"date": '1-Jan-2015',
"count": 4
}, {
"date": '4-Jan-2015',
"count": 3
}, {
"date": '1-Feb-2015',
"count": 4
}, {
"date": '18-Feb-2015',
"count": 10
}]
console.clear()
var result = array.reduce(function(ar, item) {
var index = item.date.split('-').slice(1, 3).join(', ') //getting date Month-Year
_item = ar.filter(function(a) {
return a.date === index
})[0] // getting item if already present in array
indexOf = ar.indexOf(_item) // getting index of _item if _item is already present
if (indexOf > -1)
ar[indexOf] = {
date: index,
count: _item.count + item.count
} // we sum the count of existing _item
else
ar.push({
date: index,
count: item.count
}) // we push a new _item
return ar
}, [])
console.log(result)
document.write('<pre>' + JSON.stringify(result, null, 2) + '</pre>')