Home > Software engineering >  How to get sum of specific keys values from array of object
How to get sum of specific keys values from array of object

Time:11-24

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

CodePudding user response:

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);
<iframe name="sif1" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>

CodePudding user response:

You have a bug in your approach. You cant use string as key in array, so you need to change it to Object const violations = {}

Also before you do violations[key].speeding make sure initialized as 0 because youre trying to incerement undefined value here

CodePudding user response:

You can use array#reduce to sum up different event for same driver and company.

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, }],
     result = Object.values(violation.reduce((r, o) => {
      const key = o.driver   '_'   o.company;
      r[key] ??= {driver: o.driver, company: o.company};
      r[key][o.message] = (r[key][o.message] ?? 0)   o.value;
      return r;
     },{}));
console.log(result);
<iframe name="sif2" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>

  • Related