Search code examples
javascripthtmlsalesforcesalesforce-lightningaura.js

sum fields on js object by 2 fields and create instead of the object javascript


My goal is I have number of objects im receiving weekly, each one on these object has date, Full_Name, total_hours, and other fields. I wanna sort these object array by names and by total hours of each day. please only in javascript no jquery

example of the objects

var anArray = [{
  'End__c':"22:00",
  'Id':"Q45575",
  'Name':"W-299849",
  'Resource_Full_Name__c':"test One",
  'Start__c':"20:00",
  'date__c':"2018-02-04",
  'description__c':"rwqfrwe",
  'total_hours__c':2
},{
  'End__c':"21:00",
  'Id':"Q45551",
  'Name':"W-299809",
  'Resource_Full_Name__c':"test Two",
  'Start__c':"15:00",
  'date__c':"2018-02-01",
  'description__c':"rwqfrwe",
  'total_hours__c':5
},{
  'End__c':"20:00",
  'Id':"Q45515",
  'Name':"W-299849",
  'Resource_Full_Name__c':"test One",
  'Start__c':"10:00",
  'date__c':"2018-02-04",
  'description__c':"rwqfrwe",
  'total_hours__c':2
 }];

output should be like this, assuming sunday is 2/4

Name Total Sun Mon tue Wed fri sat

test One 6 2 4 0 0 0 0 0

test Two 3 0 3 0 0 0 0 0

This what I have

    var tmp = {}

    results.workBlockList.forEach(function (item) {
      var tempKey = item.Resource_Full_Name__c + item.date__c;
      if (!tmp.hasOwnProperty(tempKey)) {
        tmp[tempKey] = item;
      } else {
        tmp[tempKey].total_hours__c += item.total_hours__c;
      }
    });

is not working it only sorted with a date and name and not giving me only 2 list sorted by dates


Solution

  • You can use reduce function.

    Look at this code snippet

    var items = [{
    End__c:"22:00", Id:"Q45575",
     Name:"W-299849", Resource_Full_Name__c:"test One", Start__c:"20:00", 
     date__c:"2018-02-04", description__c:"rwqfrwe", total_hours__c:2
     },
     {End__c:"13:00", Id:"A155645",
     Name:"W-299849", Resource_Full_Name__c:"test One", Start__c:"9:00", 
     date__c:"2018-02-05", description__c:"rwqfrwe", total_hours__c:4
    },
    {
     End__c:"19:00", Id:"A155645",
     Name:"W-299849", Resource_Full_Name__c:"test Two", Start__c:"16:00", 
     date__c:"2018-02-05", description__c:"rwqfrwe", total_hours__c:3
     }];
    
    var result = items.reduce((a, c) => {
      var targetDay = new Date(c.date__c).getDay() === 6 ? 0 :(new Date(c.date__c).getDay() + 1);
    
      if (a[c.Resource_Full_Name__c]) {
        a[c.Resource_Full_Name__c]['week'][targetDay] += c.total_hours__c;
        a[c.Resource_Full_Name__c]['total'] += c.total_hours__c;
      } else {
        a[c.Resource_Full_Name__c] = { 'total': c.total_hours__c, 'week': new Array(7).fill(0) };
        a[c.Resource_Full_Name__c]['week'][targetDay] = c.total_hours__c;
      }
    
      return a;
    }, {});
    
    result = Object.keys(result).map((k) => ({'workblock': {...result[k], ...{'resource': k}}}))
    
    console.log(result);
    .as-console-wrapper {
      max-height: 100% !important
    }