JavaScript
var data = [
{"market": "Qacha's nek","commodity": 55,"price": "90","month": "04","year": "2017"},
{"market": "Mohales Hoek","commodity": 55,"price": "75","month": "04","year": "2017"},
{"market": "Mafeteng","commodity": 55,"price": "75","month": "04","year": "2017"},
{"market": "Maseru","commodity": 55,"price": "69","month": "04","year": "2017"},
{"market": "Butha-Buthe","commodity": 55,"price": "66","month": "04","year": "2017"},
{"market": "Leribe","commodity": 56,"price": "64","month": "04","year": "2017"},
{"market": "Butha-Buthe","commodity": 55,"price": "65","month": "04","year": "2017"},
{"market": "Thaba-Tseka","commodity": 55,"price": "82","month": "04","year": "2017"},
{"market": "Thaba-Tseka","commodity": 55,"price": "81","month": "04","year": "2017"},
{"market": "Maseru","commodity": 56,"price": "74,99","month": "04","year": "2017"}
];
keys = ['market', 'commodity', 'month', 'year'],
count = {},
result = data.reduce(function (r, o) {
var key = keys.map(function (k) { return o[k]; }).join('|');
if (!count[key]) {
count[key] = { count: 1, sum: +o.price.replace(',', '.'), data: JSON.parse(JSON.stringify(o)) };
count[key].data.count = 1;
r.push(count[key].data);
} else {
count[key].sum += +o.price.replace(',', '.');
count[key].data.count+=1;
count[key].data.price = (count[key].sum / count[key].data.count).toString();
}
return r;
}, []);
console.log(result);