按两个元素和值对数组对象进行分组 (Javascript)

问题描述 投票:1回答:1

我有以下情况。

我有一个由当前登录用户的所有预约组成的数组,像这样。

 this.appointments = [  
{ 
 CreatedBy: "bob",
 Description: "smthng",
 EndTime: "2020-06-01T17:00:00.000Z",
 EndTimezone: null,
 Id: 3,
 IsAllDay: false,
 Client: "Steven",
 Location: "smthng",
 RecurrenceRule: "FREQ=DAILY;INTERVAL=1;COUNT=5;",
 StartTime: "2020-06-01T09:00:00.000Z",
 StartTimezone: null,
 Subject: "smthng",
 km: 88,
 week: 23,
 _id: "5ecc6f4a08c79c6328974699"
}, 
{ 
 CreatedBy: "bob",
 Description: "smthng",
 EndTime: "2020-06-08T10:30:00.000Z",
 EndTimezone: null,
 Id: 4,
 IsAllDay: false,
 Client: "Steven",
 Location: "smthng",
 RecurrenceRule: null,
 StartTime: "2020-06-08T10:00:00.000Z",
 StartTimezone: null,
 Subject: "smthng"  ,
 km: 88,
 week: 24,
 _id: "5ed450d299d5303bd0338a7f"
}
  ] 

我最终想做的是把数组按客户每周的情况分组 所以把所有的时间和 km 的预约(使用dayjs库),每个客户的一周。这是 .reduce 我现在有一个函数,数组只由客户端,但还没有每个星期。

this.appointments.reduce(function (res, value) {
  let diff = dayjs(value.EndTime).diff(dayjs(value.StartTime), "hour",true) % 60;
  let count;

  if(value.RecurrenceRule != null) {
    count =  parseInt(value.RecurrenceRule.split("COUNT=")[1]);
    if(value.RecurrenceException){
    if(value.RecurrenceException.match(/,/g) ) {
      if(value.RecurrenceException.match(/,/g).length == 0) {
        console.log(value.RecurrenceException.match(/,/g).length);
        count = count -1;
      }
    } else if(value.RecurrenceException && value.RecurrenceException.match(/,/g).length == 1) {
      console.log(value.RecurrenceException.match(/,/g).length);      
      count = count -2;
    } else if(value.RecurrenceException && value.RecurrenceException.match(/,/g).length == 2) {
      console.log(value.RecurrenceException.match(/,/g).length);
      count = count -3;
    } else
    if(value.RecurrenceException && value.RecurrenceException.match(/,/g).length == 3) {
       count = count -4;
    };
  }
  }
  else if(value.RecurrenceRule == null) {
       count = 1;
  }
  if (!res[value.Client]) {
    res[value.Client] = {

      week: value.week,
      km: value.km * count,
      Client: value.Client,
      count: count,
      difference: diff * count
    };
    result.push(res[value.Client]);


  } else {
    res[value.Client].km += value.km * count;
    res[value.Client].difference += diff * count;
  }

  return res;
}, {});

我如何处理这种情况? 现在的输出是,而不是创建两行(周23& 24),它总结了所有的约会史蒂文在第23周是不正确的,因为0.5小时发生在另一个星期。

{Client: "Steven"
count: 5
difference: 40.5
km: 528
week: 23}

所以理想的输出是。

{
Client: "Steven"
count: 5
difference: 40
km: 440
week: 23 
},

{
Client: "Steven"
count: 1
difference: 0.5
km: 88
week: 24
}

你可以有相同的周数的行,只要客户端不同。

我希望我做的很清楚,如果你需要更多的上下文,请提

javascript arrays typescript object reduce
1个回答
1
投票

在@ajai的回答基础上(首先使用他的输入进行比较),希望能给你你需要的东西(并使用reduce)。

const data = [  
  { 
   CreatedBy: "bob",
   Description: "smthng",
   EndTime: "2020-06-01T17:00:00.000Z",
   EndTimezone: null,
   Id: 3,
   IsAllDay: false,
   Client: "Steven",
   Location: "smthng",
   RecurrenceRule: "FREQ=DAILY;INTERVAL=1;COUNT=5;",
   StartTime: "2020-06-01T09:00:00.000Z",
   StartTimezone: null,
   Subject: "smthng",
   km: 88,
   week: 23,
   _id: "5ecc6f4a08c79c6328974699"
  }, 
  { 
   CreatedBy: "bob",
   Description: "smthng",
   EndTime: "2020-06-08T10:30:00.000Z",
   EndTimezone: null,
   Id: 4,
   IsAllDay: false,
   Client: "Steven",
   Location: "smthng",
   RecurrenceRule: null,
   StartTime: "2020-06-08T10:00:00.000Z",
   StartTimezone: null,
   Subject: "smthng"  ,
   km: 88,
   week: 24,
   _id: "5ed450d299d5303bd0338a7f"
  },
  { 
   CreatedBy: "bob",
   Description: "smthng",
   EndTime: "2020-06-01T17:00:00.000Z",
   EndTimezone: null,
   Id: 3,
   IsAllDay: false,
   Client: "Steven",
   Location: "smthng",
   RecurrenceRule: "FREQ=DAILY;INTERVAL=1;COUNT=5;",
   StartTime: "2020-06-01T09:00:00.000Z",
   StartTimezone: null,
   Subject: "smthng",
   km: 40,
   week: 23,
   _id: "5ecc6f4a08c79c6328974699"
  },
  { 
   CreatedBy: "bob",
   Description: "smtng",
   EndTime: "2020-06-01T17:00:00.000Z",
   EndTimezone: null,
   Id: 3,
   IsAllDay: false,
   Client: "ajai",
   Location: "smthng",
   RecurrenceRule: "FREQ=DAILY;INTERVAL=1;COUNT=5;",
   StartTime: "2020-06-01T09:00:00.000Z",
   StartTimezone: null,
   Subject: "smthng",
   km: 88,
   week: 23,
   _id: "5ecc6f4a08c79c6328974699"
  }
]    
  

const result = Object.values(data.reduce((aggObj, item) => {
  const stringID = `${item.Client}_${item.week}`;
  const diff = (new Date(item.EndTime) - new Date(item.StartTime))/(1000*60*60);
  const RecurrenceObj = item.RecurrenceRule ?
    item.RecurrenceRule.split(";").map(a => {
      //console.log(a)
      return a.split("=") || ["_", null];          
    }).reduce((aggObj, [key,val]) => {
      aggObj[key] = val;
      return aggObj;
    })
    : {COUNT: 1};
    
  if (aggObj[stringID]){
    aggObj[stringID].km += (item.km * RecurrenceObj.COUNT);
    aggObj[stringID].count += parseInt(RecurrenceObj.COUNT);
    aggObj[stringID].difference += (diff * RecurrenceObj.COUNT);
  }
  else {  
    aggObj[stringID] = {
      Client: item.Client,
      km: item.km * RecurrenceObj.COUNT,
      count: parseInt(RecurrenceObj.COUNT),
      difference: diff * RecurrenceObj.COUNT,
      week: item.week
    };
  }
  return aggObj;
}, {}));

/*
for(var i=0;i<=data.length-1;i++){
   let pos = result.findIndex(el=> `${el.Client}-${el.week}`==`${data[i].Client}-${data[i].week}`)
   if(pos==-1){
     result.push({Client: data[i]['Client'],count: 1,difference: 0,km: data[i]['km'],week: data[i]['week']})
   }else{
     result[pos]['count'] = result[pos]['count'] + 1;
     result[pos]['km'] = result[pos]['km'] + data[i]['km'];
   }
}
*/
console.log(result)
.as-console-wrapper { max-height: 100% !important; top: 0; }

OUTPUT。

[
  {
    "Client": "Steven",
    "km": 640,
    "count": 10,
    "difference": 80,
    "week": 23
  },
  {
    "Client": "Steven",
    "km": 88,
    "count": 1,
    "difference": 0.5,
    "week": 24
  },
  {
    "Client": "ajai",
    "km": 440,
    "count": 5,
    "difference": 40,
    "week": 23
  }
]

现在用你的精确输入。

const data = [  
  { 
   CreatedBy: "bob",
   Description: "smthng",
   EndTime: "2020-06-01T17:00:00.000Z",
   EndTimezone: null,
   Id: 3,
   IsAllDay: false,
   Client: "Steven",
   Location: "smthng",
   RecurrenceRule: "FREQ=DAILY;INTERVAL=1;COUNT=5;",
   StartTime: "2020-06-01T09:00:00.000Z",
   StartTimezone: null,
   Subject: "smthng",
   km: 88,
   week: 23,
   _id: "5ecc6f4a08c79c6328974699"
  }, 
  { 
   CreatedBy: "bob",
   Description: "smthng",
   EndTime: "2020-06-08T10:30:00.000Z",
   EndTimezone: null,
   Id: 4,
   IsAllDay: false,
   Client: "Steven",
   Location: "smthng",
   RecurrenceRule: null,
   StartTime: "2020-06-08T10:00:00.000Z",
   StartTimezone: null,
   Subject: "smthng"  ,
   km: 88,
   week: 24,
   _id: "5ed450d299d5303bd0338a7f"
  }
]    
  

const result = Object.values(data.reduce((aggObj, item) => {
  const stringID = `${item.Client}_${item.week}`;
  const diff = (new Date(item.EndTime) - new Date(item.StartTime))/(1000*60*60);
  const RecurrenceObj = item.RecurrenceRule ?
    item.RecurrenceRule.split(";").map(a => {
      //console.log(a)
      return a.split("=") || ["_", null];
    }).reduce((aggObj, [key,val]) => {
      aggObj[key] = val;
      return aggObj;
    })
    : {COUNT: 1};
    
  if (aggObj[stringID]){
    aggObj[stringID].km += (item.km * RecurrenceObj.COUNT);
    aggObj[stringID].count += parseInt(RecurrenceObj.COUNT);
    aggObj[stringID].difference += (diff * RecurrenceObj.COUNT);
  }
  else {  
    aggObj[stringID] = {
      Client: item.Client,
      km: item.km * RecurrenceObj.COUNT,
      count: parseInt(RecurrenceObj.COUNT),
      difference: diff * RecurrenceObj.COUNT,
      week: item.week
    };
  }
  return aggObj;
}, {}));

/*
for(var i=0;i<=data.length-1;i++){
   let pos = result.findIndex(el=> `${el.Client}-${el.week}`==`${data[i].Client}-${data[i].week}`)
   if(pos==-1){
     result.push({Client: data[i]['Client'],count: 1,difference: 0,km: data[i]['km'],week: data[i]['week']})
   }else{
     result[pos]['count'] = result[pos]['count'] + 1;
     result[pos]['km'] = result[pos]['km'] + data[i]['km'];
   }
}
*/
console.log(result)
.as-console-wrapper { max-height: 100% !important; top: 0; }

OUTPUT:

[
  {
    "Client": "Steven",
    "km": 440,
    "count": 5,
    "difference": 40,
    "week": 23
  },
  {
    "Client": "Steven",
    "km": 88,
    "count": 1,
    "difference": 0.5,
    "week": 24
  }
]

1
投票

我不确定这是否符合你的期望,我也不确定该如何处理差异,如果不满意的话,请解释给我听。

var data = [  
{ 
 CreatedBy: "bob",
 Description: "smthng",
 EndTime: "2020-06-01T17:00:00.000Z",
 EndTimezone: null,
 Id: 3,
 IsAllDay: false,
 Client: "Steven",
 Location: "smthng",
 RecurrenceRule: "FREQ=DAILY;INTERVAL=1;COUNT=5;",
 StartTime: "2020-06-01T09:00:00.000Z",
 StartTimezone: null,
 Subject: "smthng",
 km: 88,
 week: 23,
 _id: "5ecc6f4a08c79c6328974699"
}, 
{ 
 CreatedBy: "bob",
 Description: "smthng",
 EndTime: "2020-06-08T10:30:00.000Z",
 EndTimezone: null,
 Id: 4,
 IsAllDay: false,
 Client: "Steven",
 Location: "smthng",
 RecurrenceRule: null,
 StartTime: "2020-06-08T10:00:00.000Z",
 StartTimezone: null,
 Subject: "smthng"  ,
 km: 88,
 week: 24,
 _id: "5ed450d299d5303bd0338a7f"
},
{ 
 CreatedBy: "bob",
 Description: "smthng",
 EndTime: "2020-06-01T17:00:00.000Z",
 EndTimezone: null,
 Id: 3,
 IsAllDay: false,
 Client: "Steven",
 Location: "smthng",
 RecurrenceRule: "FREQ=DAILY;INTERVAL=1;COUNT=5;",
 StartTime: "2020-06-01T09:00:00.000Z",
 StartTimezone: null,
 Subject: "smthng",
 km: 40,
 week: 23,
 _id: "5ecc6f4a08c79c6328974699"
},
{ 
 CreatedBy: "bob",
 Description: "smtng",
 EndTime: "2020-06-01T17:00:00.000Z",
 EndTimezone: null,
 Id: 3,
 IsAllDay: false,
 Client: "ajai",
 Location: "smthng",
 RecurrenceRule: "FREQ=DAILY;INTERVAL=1;COUNT=5;",
 StartTime: "2020-06-01T09:00:00.000Z",
 StartTimezone: null,
 Subject: "smthng",
 km: 88,
 week: 23,
 _id: "5ecc6f4a08c79c6328974699"
}
  ]    
let result = []
for(var i=0;i<=data.length-1;i++){
   let pos = result.findIndex(el=> `${el.Client}-${el.week}`==`${data[i].Client}-${data[i].week}`)
   if(pos==-1){
     result.push({Client: data[i]['Client'],count: 1,difference: 0,km: data[i]['km'],week: data[i]['week']})
   }else{
     result[pos]['count'] = result[pos]['count'] + 1;
     result[pos]['km'] = result[pos]['km'] + data[i]['km'];
   }
}
console.log(result)

 
© www.soinside.com 2019 - 2024. All rights reserved.