Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to get sum of specific keys values from array of object

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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", 
}]

>Solution :

Array.reduce will help you.

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, }];
const result = violation.reduce((acc, curr) => {
  const node = acc.find(item => item.driver === curr.driver && item.company === curr.company);
  if (node) node[curr.message] ? node[curr.message] += curr.value : node[curr.message] = curr.value
  else
    acc.push({
      driver: curr.driver,
      company: curr.company,
      [curr.message]: curr.value
    });
  return acc;
}, []);
console.log(result);
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading