如何根据fromTime和toTime对对象数组进行分组?

时间:2018-01-29 10:24:57

标签: javascript

我尝试使用lodash groupBy()但它只接受一个参数。

这是一个示例数据:

let data = [
 {sign: 'open', day: 'mon', fromTime :'1:00 pm', toTime: '3:00 pm'},
 {sign: 'open', day: 'tue', fromTime :'2:00 pm', toTime: '6:00 pm'},
 {sign: 'open', day: 'wed', fromTime :'1:00 pm', toTime: '3:00 pm'},
 {sign: 'open', day: 'thu', fromTime :'2:00 pm', toTime: '6:00 pm'}];

我想像这样分组:

let updated = [
 { 
  days : ['mon','wed'], 
  time : { fromTime: '1:00 pm', toTime: '3:00 pm'},
  sign:'open'
 },
 { 
  days : ['tue','thu'], 
  time : { fromTime: '2:00 pm', toTime: '6:00 pm'},
  sign:'open'
 },
]

我需要使用fromTime和toTime对它进行分组:D

3 个答案:

答案 0 :(得分:0)

您可以使用array.prototype.reduce



let data = [
 {sign: 'open', day: 'mon', fromTime :'1:00 pm', toTime: '3:00 pm'},
 {sign: 'open', day: 'tue', fromTime :'2:00 pm', toTime: '6:00 pm'},
 {sign: 'open', day: 'wed', fromTime :'1:00 pm', toTime: '3:00 pm'},
 {sign: 'open', day: 'thu', fromTime :'2:00 pm', toTime: '6:00 pm'}];
 
let result = data.reduce((m, o) => {
  var found = m.find(e => e.time.fromTime === o.fromTime && e.time.toTime === o.toTime);
  found ? found.days.push(o.day) : m.push({days: [o.day], time: {fromTime: o.fromTime, toTime: o.toTime}, sign: o.sign});
  return m;
}, []);

console.log(result);




使用destructuring

缩短了时间



let data = [
 {sign: 'open', day: 'mon', fromTime :'1:00 pm', toTime: '3:00 pm'},
 {sign: 'open', day: 'tue', fromTime :'2:00 pm', toTime: '6:00 pm'},
 {sign: 'open', day: 'wed', fromTime :'1:00 pm', toTime: '3:00 pm'},
 {sign: 'open', day: 'thu', fromTime :'2:00 pm', toTime: '6:00 pm'}];
 
let result = data.reduce((m, {sign, day, fromTime, toTime}) => {
  var found = m.find(({time}) => time.fromTime === fromTime && time.toTime === toTime);
  found ? found.days.push(day) : m.push({days: [day], time: {fromTime, toTime}, sign});
  return m;
}, []);

console.log(result);




答案 1 :(得分:0)

您可以为相同的组使用哈希表,为组的键使用数组。

然后迭代并检查哈希表中是否存在密钥并分配新的结果集。后来把这一天推到了同一个小组。



let data = [{ sign: 'open', day: 'mon', fromTime :'1:00 pm', toTime: '3:00 pm' }, { sign: 'open', day: 'tue', fromTime :'2:00 pm', toTime: '6:00 pm' }, { sign: 'open', day: 'wed', fromTime :'1:00 pm', toTime: '3:00 pm' }, { sign: 'open', day: 'thu', fromTime :'2:00 pm', toTime: '6:00 pm' }],
    groups = ['fromTime', 'toTime'],
    grouped = [],
    hash = Object.create(null);
    
data.forEach(function (o) {
    var key = groups.map(k => o[k]).join('|');
    if (!hash[key]) {
        hash[key] = { days: [], time: { fromTime: o.fromTime, toTime: o.toTime }, sign: o.sign };
        grouped.push(hash[key]);
    }
    hash[key].days.push(o.day);
});

console.log(grouped);

.as-console-wrapper { max-height: 100% !important; top: 0; }




答案 2 :(得分:0)

您还可以通过组合:

编写自己的groupBy方法
  • filter(删除所有未使用的元素)
  • map(更改结构)
  • reduce(结合天数)

let data = [
 {sign: 'open', day: 'mon', fromTime :'1:00 pm', toTime: '3:00 pm'},
 {sign: 'open', day: 'tue', fromTime :'2:00 pm', toTime: '6:00 pm'},
 {sign: 'open', day: 'wed', fromTime :'1:00 pm', toTime: '3:00 pm'},
 {sign: 'open', day: 'thu', fromTime :'2:00 pm', toTime: '6:00 pm'}];

function groupBy(fromTime, toTime) {

    return data
    .filter(d => d.fromTime === fromTime && d.toTime === toTime)
    .map(e => {
      return {
         days: [e.day],
         time: {
             fromTime: e.fromTime,
             toTime: e.toTime
         },
         sign: e.sign
      }
    })
    .reduce((f, s) => { f.days.push(s.days[0]); return f; });
}

let groupedData = [];
groupedData.push(groupBy('1:00 pm', '3:00 pm'));
groupedData.push(groupBy('2:00 pm', '6:00 pm'));

console.log(groupedData);