0

我的目标是我每周收到一些对象,这些对象上的每个对象都有日期、Full_Name、total_hours 和其他字段。我想按名称和每天的总小时数对这些对象数组进行排序。请只在javascript没有jquery

对象示例

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
 }];

输出应该是这样的,假设星期天是 2/4

姓名 总计 周日 周一 周二 周三 周五 周六

测试一 6 2 4 0 0 0 0 0

测试二 3 0 3 0 0 0 0 0

这就是我所拥有的

    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;
      }
    });

不起作用,它只按日期和名称排序,而不是只给我 2 个按日期排序的列表

4

2 回答 2

2

你可以使用reduce函数。

看看这个代码片段

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
}

于 2018-02-06T02:25:15.777 回答
0

试试这个:

   
    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':"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-14",
      'description__c':"rwqfrwe",
      'total_hours__c':2
     }];
     
    var daysHours = {};
    
    items.forEach(function(item){
      var d = new Date(item.date__c);
      var dayOfWeek = d.getDay();
      
      if (typeof daysHours[item.Resource_Full_Name__c] === 'undefined') {
      		daysHours[item.Resource_Full_Name__c] = [0,0,0,0,0,0,0];
      }
      
      daysHours[item.Resource_Full_Name__c][dayOfWeek] += item.total_hours__c;
      
    });
    
    var body = document.getElementById('body');
    
    for (var i in daysHours) {
      var sum = 0;
      daysHours[i].forEach(function(item) {
       sum += item;
      });
      daysHours[i].splice(0, 0, sum);
      body.innerHTML += '<div>"'+i+'" '+daysHours[i]+'</div>';
    }
<div id='body'></div>

输出:

"test One" 4,2,0,0,2,0,0,0
"test Two" 5,0,0,0,0,5,0,0
于 2018-02-06T01:27:01.333 回答