JSON 计算起始日期和截止日期之间所有日期的所有出现次数

JSON with counts for all occurrences for all dates between a from and to date

我有以下对象数组

const monthlyList =  [
       {
          "leave_id":119,
          "employee_id":"25528",
          "from_date":"2022-03-10",
          "to_date":"2022-03-11",
          "employee_name":"Name 1"
       },
       {
          "leave_id":120,
          "employee_id":"25529",
          "from_date":"2022-03-10",
          "to_date":"2022-03-13",
          "employee_name":"Name 2"
       },
       {
          "leave_id":121,
          "employee_id":"25530",
          "from_date":"2022-03-15",
          "to_date":"2022-03-18",
          "employee_name":"Name 3"
       },
       {
          "leave_id":123,
          "employee_id":"25530",
          "from_date":"2022-03-20",
          "to_date":"2022-03-20",
          "employee_name":"Name 3"
       }
    ]

我正在尝试使用 FullCalendar 在日历中显示每天的请假事件数。为此,我尝试获取以下格式的数组

[
   {
      "date":"2022-03-10",
      "count":2
   },
   {
      "date":"2022-03-11",
      "count":2
   },
   {
      "date":"2022-03-12",
      "count":1
   }
   --- etc
]

也就是说,如果同一天有两片叶子,计数应该是 2。另外,它应该计算 from_date 和 to_date 之间的所有日期的计数,所以我可以在日历上显示该范围内所有日期的计数。

我使用以下代码使用 lodash 基于 from_dateto_date 进行分组,然后我可以从那里创建上面的数组。但不知道如何在中间的日子里做到这一点

let groupedListFrom = _.groupBy(monthlyList, "from_date");
let groupedListTo   = _.groupBy(monthlyList, "to_date");

如果有人能在这里帮助我,那将非常有帮助。谢谢

一种使用 reduce 而不是 Lodash 的可能方法。
我使用 https://gist.github.com/miguelmota/7905510 作为助手来获取两个给定日期之间的 Date 对象。

const monthlyList =  [
       {
          "leave_id":119,
          "employee_id":"25528",
          "from_date":"2022-03-10",
          "to_date":"2022-03-11",
          "employee_name":"Name 1"
       },
       {
          "leave_id":120,
          "leave_id":"25529",
          "from_date":"2022-03-10",
          "to_date":"2022-03-13",
          "employee_name":"Name 2"
       },
       {
          "leave_id":121,
          "employee_id":"25530",
          "from_date":"2022-03-15",
          "to_date":"2022-03-18",
          "employee_name":"Name 3"
       },
       {
          "leave_id":123,
          "employee_id":"25530",
          "from_date":"2022-03-20",
          "to_date":"2022-03-20",
          "employee_name":"Name 3"
       }
    ]
 
 //a function to get Date objects between two given dates (inclusive)
 //https://gist.github.com/miguelmota/7905510
 const getDates = (startDate, endDate) => {
  const dates = []
  let currentDate = startDate
  const addDays = function (days) {
    const date = new Date(this.valueOf())
    date.setDate(date.getDate() + days)
    return date
  }
  while (currentDate <= endDate) {
    dates.push(currentDate)
    currentDate = addDays.call(currentDate, 1)
  }
  return dates
}


let x = Object.values(monthlyList.reduce((acc,{leave_id,employee_id,from_date,to_date,employee_name})=>{
    const dates = getDates(new Date(from_date), new Date(to_date));
  dates.forEach((date) => {
    let strippedDate = date.toISOString().substring(0,10);
    if(!acc[strippedDate])acc[strippedDate]={date:strippedDate,count:0};
    acc[strippedDate].count+=1

    })
  return acc;
  
},{}))

console.log(x)

var DateTime = require('luxon').DateTime;

let leavesArray = {};
monthlyList.forEach(x => {
let a =  DateTime.fromFormat(x.from_date,"yyyy-MM-dd")
let b =  DateTime.fromFormat(x.to_date,"yyyy-MM-dd")
while (a <= b) {
    let date = a.toFormat("yyyy-MM-dd");
    if (leavesArray[date]) {
        leavesArray[date]["count"]++;
    } else {
        leavesArray[date] = {}
        leavesArray[date]["count"] = 1;
    }        a = a.plus({days: 1})
   } 
  })

Node Js 工作解决方案