I have similar to the following array of objects:
let violation = [
{
"timestamp": 1637658787661,
"driver": "7f5fda5d-86a8-471e-ae32-fcc39deb8fc7",
"company": "9d8616dd-4689-4689-8812-a2345ccdcfc5",
"message": "CreateHarshAccelerationEvent",
"value": 71,
},
{
"timestamp": 1637658789678,
"driver": "7f5fda5d-86a8-471e-ae32-fcc39deb8fc7",
"company": "9d8616dd-4689-4689-8812-a2345ccdcfc5",
"message": "CreateHarshDecelerationEvent",
"value": 50,
},
{
"timestamp": 1637659776571,
"driver": "7f5fda5d-86a8-471e-ae32-fcc39deb8fc7",
"company": "9d8616dd-4689-4689-8812-a2345ccdcfc5",
"message": "CreateIdlingEvent",
"value": 0,
},
{
"timestamp": 1637660707375,
"driver": "7f5fda5d-86a8-471e-ae32-fcc39deb8fc7",
"company": "9d8616dd-4689-4689-8812-a2345ccdcfc5",
"message": "CreateSpeedingEvent",
"value": 67,
},
{
"timestamp": 1637661519707,
"driver": "7f5fda5d-86a8-471e-ae32-fcc39deb8fc7",
"company": "9d8616dd-4689-4689-8812-a2345ccdcfc5",
"message": "CreateHarshAccelerationEvent",
"value": 71,
},
{
"timestamp": 1637661521773,
"driver": "7f5fda5d-86a8-471e-ae32-fcc39deb8fc7",
"company": "9d8616dd-4689-4689-8812-a2345ccdcfc5",
"message": "CreateHarshDecelerationEvent",
"value": 50,
},
{
"timestamp": 1637661548282,
"driver": "7f5fda5d-86a8-471e-ae32-fcc39deb8fc7",
"company": "9d8616dd-4689-4689-8812-a2345ccdcfc5",
"message": "CreateSpeedingEvent",
"value": 62,
},
{
"timestamp": 1637663230199,
"driver": "7f5fda5d-86a8-471e-ae32-fcc39deb8fc7",
"company": "9d8616dd-4689-4689-8812-a2345ccdcfc5",
"message": "CreateSpeedingEvent",
"value": 66,
},
.....
.....
]
So here, Each of the object will have a message property or Key of one of four values namely CreateHarshAccelerationEvent, CreateHarshDecelerationEvent, CreateSpeedingEvent, CreateIdlingEvent.
Here I need to get the total of each key. i.e. total of CreateHarshAccelerationEvent and total of CreateHarshDecelerationEvent and total of CreateIdlingEvent and total of CreateSpeedingEvent
But the condition I have to select the object for total with the identifier, and that identifier is the combination of ID of driver and company i.e.
key = company + "/" + driver
I am doing the following code for this
var violations = []
for (let violation of data){
var key
if ( (violation.company) && (violation.driver) ) {
key = violation.company + "/" + violation.driver;
let violationType = violation.message;
violations[key].company = violation.company;
violations[key].driver = violation.driver;
}
}
console.log(violations);
I need result as
[{
"driver": "7f5fda5d-86a8-471e-ae32-fcc39deb8fc7",
"company": "9d8616dd-4689-4689-8812-a2345ccdcfc5",
"CreateHarshAccelerationEvent": "total number of values CreateHarshAccelerationEvent from Array of objects basis on the basis of that custom key i.e. company/driver value",
"CreateHarshDecelerationEvent": "total number of values CreateHarshDecelerationEvent from Array of objects basis on the basis of that custom key i.e. company/driver value",
"CreateIdlingEvent": "total number of values CreateIdlingEvent from Array of objects basis on the basis of that custom key i.e. company/driver value",
"CreateSpeedingEvent": "total number of values CreateSpeedingEvent from Array of objects basis on the basis of that custom key i.e. company/driver value",
}]