Javascript:按日期分组数据并填写其间的日期

时间:2017-12-09 06:10:29

标签: javascript arrays json javascript-objects

我有一个对象的事件数据数组,只有每个事件的结束和开始日期。

let listOfEvents = [{
  "end": Date 2017-12-09T11:12:00.000Z,
  "start": Date 2017-12-14T03:12:00.000Z,
  "title": "Event One"
},{
  "end": Date 2017-11-10T11:12:00.000Z,
  "start": Date 2017-11-12T03:12:00.000Z,
  "title": "Event Two"
}]

这就是我想要的最终输出

    let listOfEvents = [{
      "date": Date 2017-12-09T11:12:00.000Z,
      "title": "Event One"
    },{
      "date": Date 2017-12-10T11:12:00.000Z,
      "title": "Event One"
    }{
      "date": Date 2017-12-11T11:12:00.000Z,
      "title": "Event One"
    }{
      "date": Date 2017-12-12T11:12:00.000Z,
      "title": "Event One"
    }{
      "date": Date 2017-12-13T03:12:00.000Z,
      "title": "Event One"
    }{
      "date": Date 2017-12-14T11:12:00.000Z,
      "title": "Event One"
    },{
      "date": Date 2017-11-10T11:12:00.000Z,
      "title": "Event Two"
    },{
      "date": Date 2017-11-11T03:12:00.000Z,
      "title": "Event Two"
    },{
      "date": Date 2017-11-12T03:12:00.000Z,
      "title": "Event Two"
    }]

这就是我目前所拥有的,

let newEvents = [];    
listOfEvents.forEach(function (item) {
        newEvents.push({
            date: item.start,
            title: item.title
        })
    });

我如何添加逻辑来填充当天并为每个新条目添加一天到结束日期然后继续循环?如果需要,我可以包括momentjs。

2 个答案:

答案 0 :(得分:2)

您可以使用array#reduce并比较日期,并将每个新的一天添加到结果数组,直到结束日期小于开始日期。

let listOfEvents = [{ "end": "2017-12-09T11:12:00.000Z", "start": "2017-12-14T03:12:00.000Z", "title": "Event One" },{ "end": "2017-11-10T11:12:00.000Z", "start": "2017-11-12T03:12:00.000Z", "title": "Event Two" }];

var result = listOfEvents.reduce((r, {
  end,
  start,
  title
}) => {
  end = new Date(end);
  start = new Date(start);

  while (end < start) {
    r.push({
      'date': new Date(end),
      title
    });
    end.setDate(end.getDate() + 1);
  }
  if (Math.abs(start.getTime() - end.getTime()) < 1000 * 3600 * 24) {
    r.push({
      'date': new Date(start),
      title
    });
  }

  return r;
}, []);
console.log(result);

答案 1 :(得分:1)

您可以提取基于给定输入创建范围的函数,然后将事件减少为新数组

请注意,根据给定的数据,startend属性似乎已被反转,并且给定的问题并未说明它是否应该在日期或日期时间进行纯粹比较。如果@Hassans answer在准备好时将一个项目添加到数据集中,我特别忽略了这一点,因为我认为这符合规范

&#13;
&#13;
const extractDateRange = ( range, transform ) => {
  let results = [];
  // note that start and end date seem to be up side down in the question
  let curDate = new Date(range.end);
  let endDate = new Date(range.start);
  while (curDate <= endDate) {
    results.push( transform( new Date(curDate), range ) );
    curDate.setDate( curDate.getDate() + 1 ); // / add 1 day
  }
  return results;
};

let listOfEvents = [{
  "end": '2017-12-09T11:12:00.000Z',
  "start": '2017-12-14T03:12:00.000Z',
  "title": "Event One"
},{
  "end": '2017-11-10T11:12:00.000Z',
  "start": '2017-11-12T03:12:00.000Z',
  "title": "Event Two"
}];

let allEvents = listOfEvents.reduce( (current, item) => current.concat( 
  extractDateRange( item, ( date, props ) => ({ 
    title: props.title, 
    date 
  }) ) )
, []);

console.log( allEvents );
&#13;
&#13;
&#13;