【问题标题】:Reduce and group by condition按条件减少和分组
【发布时间】:2020-05-11 13:25:46
【问题描述】:

我有一个函数可以计算同时发生的最大事件 (HH:mm)。下面的示例同时发生了三个事件。我想像下面的例子一样对事件进行分组。

结果应该是:

数字会议 00:00-01.00

  • 第 1 组:
    支持00.00-00.25

  • 第 2 组:
    预约会议 00:30-01:30
    个人会议 00:30 - 01:30

到目前为止,我只能找到一个事件的发生情况。我想我必须使用减速器和条件?

let events = [
  {
    id: 9,
    start: 0,
    end: 60,
    title: "Digital meeting"
  },
  {
    id: 80,
    start: 30,
    end: 90,
    title: "Book meeting"
  },
  {
    id: 81,
    start: 30,
    end: 90,
    title: "Personal meeting"
  },
  {
    id: 84,
    start: 0,
    end: 24,
    title: "Support"
  }]

 events.forEach(item => {

// Find the matches
const matchedItems = events.filter(o => {
  return o.start <= item.end && o.end >= item.start && item.id !== o.id;
});

console.log(`Matched for ${item.id}`, matchedItems)

// Remove matches from the events array so there won't be duplicates
//remove(matchedItems);

// Format the items and place them in calendar grid
//format([item, ...matchedItems]);
   });

【问题讨论】:

    标签: javascript arrays css-grid reduce


    【解决方案1】:

    我为你实现了一个功能,代码见下,不客气

    // calculate if 2 events overlap, no matter of their provided order
    function overlap(eventA, eventB) {
    
      // sort input
      const [first, second] = [eventA, eventB].sort((a, b) => a.start - b.start);
      
      // check if events overlap
      const overlap = first.end > second.start;
      return overlap;
    }
    
    // create a map object with the groups
    // also sorted by time
    // each group contains the event objects
    function findOverlaps(events, query) {
    
      // result
      const groups = {};
      
      // event to build the groups for
      const queryEvent = events.find(e => e.title === query);
      
      // the other events to check of they overlap with the queried event
      const eventsWithoutQuery = events.filter(e => e.title !== query);
      eventsWithoutQuery.forEach(event => {
        const overlaps = overlap(event, queryEvent);
        
        // if there is an overlap
        if (overlaps) {
          // construct the groupname
          const groupName = `${event.start}-${event.end}`;
    
          // if the group alread exists use it, otherwise create an empty group (array)
          const group = groups[groupName] ? groups[groupName] : [];
          group.push(event);
          
          // put the group back to the groups map and name it accordingly
          groups[groupName] = group;
        }
      });
      
      // make sure the keys in the group are sorted (optional)
      const sortedKeys = Object.keys(groups).sort();
      const sortedGroups = sortedKeys.reduce(
        (acc, curr) => ({
          ...acc,
          [curr]: groups[curr]
        }),
        {}
      );
      
      // return sorted map
      return sortedGroups;
    }
    
    
    // the events
    let events = [
        {
          id: 9,
          start: 0,
          end: 60,
          title: "Digital meeting"
        },
        {
          id: 80,
          start: 30,
          end: 90,
          title: "Book meeting"
        },
        {
          id: 81,
          start: 30,
          end: 90,
          title: "Personal meeting"
        },
        {
          id: 84,
          start: 0,
          end: 24,
          title: "Support"
        }
      ];
     
    
      console.log("Iterate over Digital meeting groups");
      // construct the groups for "Digial meeting" 
      const groups = findOverlaps(events, "Digital meeting");
    
      // iterate over overlap maps of Digital meeting
      for (const [groupName, eventsOfGroup] of Object.entries(groups)) {
      
        console.log(groupName, eventsOfGroup);
      }
      
      console.log("Iterate over everything");
      events.forEach(e => {   
        console.log("Groups for: " + e.title);
        for (const [groupName, eventsOfGroup] of Object.entries(findOverlaps(events, e.title))) {  
          console.log(groupName, eventsOfGroup);
        }  
      });
      
      

    【讨论】:

      猜你喜欢
      • 2021-08-07
      • 2021-03-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-12-10
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多