just not sure what is a better way?
Yes, you can use an object as a lookup map for new objects by repId:
var map = {};
for (var i=0; i<data.length; i++) {
var obj = data[i],
id = obj.repId;
if (id in map) { // we know this id already
// get the object and sum properties
map[id].profit += obj.profit;
map[id].expense += obj.expense;
} else // create a new one
map[id] = {
repId: id,
profit: obj.profit,
expense: obj.profit
};
}
/* map:
{
"35":{"repId":35,"profit":47.39,"expense":47.39},
"86":{"repId":86,"profit":179.92,"expense":132.47}
} */
// put the values from the map in an array
var otherData = [];
for (var id in map)
otherData.push(map[id]);
/* otherData:
[
{"repId":35,"profit":47.39,"expense":47.39},
{"repId":86,"profit":179.92,"expense":132.47}
] */