在 javascript 中按星期几过滤 ISO 日期数组

Filter an array of ISO date by days of the week in javascript

我有一个 ISO 日期数组,我正在尝试对其进行排序并仅从中获取接下来的 7 天

这是示例

Here is the sample code

daysdata = [{time: '2022-01-23T11:00:00Z', data: {…}},   
     {time: '2022-01-23T12:00:00Z', data: {…}},   
     {time: '2022-01-24T11:00:00Z', data: {…}},   
     {time: '2022-01-24T12:00:00Z', data: {…}},   
     {time: '2022-01-24T17:00:00Z', data: {…}},   
     {time: '2022-01-24T23:00:00Z', data: {…}},   
     {time: '2022-01-25T00:00:00Z', data: {…}},   
     {time: '2022-01-25T04:00:00Z', data: {…}},   
     {time: '2022-01-25T05:00:00Z', data: {…}},   
     {time: '2022-01-25T06:00:00Z', data: {…}},    
     {time: '2022-01-25T07:00:00Z', data: {…}},   
     {time: '2022-01-25T14:00:00Z', data: {…}},        
     {time: '2022-01-25T15:00:00Z', data: {…}},    
     {time: '2022-01-26T13:00:00Z', data: {…}},
     {time: '2022-01-26T14:00:00Z', data: {…}},
     {time: '2022-01-26T15:00:00Z', data: {…}},
     {time: '2022-01-27T13:00:00Z', data: {…}},
     {time: '2022-01-27T14:00:00Z', data: {…}},
     {time: '2022-01-27T15:00:00Z', data: {…}},
     {time: '2022-01-28T14:00:00Z', data: {…}},     
     {time: '2022-01-28T15:00:00Z', data: {…}},    
     {time: '2022-01-28T16:00:00Z', data: {…}},    
     {time: '2022-01-29T18:00:00Z', data: {…}},    
     {time: '2022-01-29T19:00:00Z', data: {…}},    
     {time: '2022-01-30T08:00:00Z', data: {…}},    
     {time: '2022-01-30T09:00:00Z', data: {…}},    
     {time: '2022-01-30T10:00:00Z', data: {…}},    
     {time: '2022-01-30T11:00:00Z', data: {…}},    
    ]

这是示例代码。

只需按 Array.sort 排序并按 Array.slice

选取数组的前 n 个元素

 

daysdata = [{time: '2022-01-23T11:00:00Z', data: {}},   
     {time: '2022-01-23T12:00:00Z', data: {}},   
     {time: '2022-01-24T11:00:00Z', data: {}},   
     {time: '2022-01-24T12:00:00Z', data: {}},   
     {time: '2022-01-24T17:00:00Z', data: {}},   
     {time: '2022-01-24T23:00:00Z', data: {}},   
     {time: '2022-01-25T00:00:00Z', data: {}},   
     {time: '2022-01-25T04:00:00Z', data: {}},   
     {time: '2022-01-25T05:00:00Z', data: {}},   
     {time: '2022-01-25T06:00:00Z', data: {}},    
     {time: '2022-01-25T07:00:00Z', data: {}},   
     {time: '2022-01-25T14:00:00Z', data: {}},        
     {time: '2022-01-25T15:00:00Z', data: {}},    
     {time: '2022-01-26T13:00:00Z', data: {}},
     {time: '2022-01-26T14:00:00Z', data: {}},
     {time: '2022-01-26T15:00:00Z', data: {}},
     {time: '2022-01-27T13:00:00Z', data: {}},
     {time: '2022-01-27T14:00:00Z', data: {}},
     {time: '2022-01-27T15:00:00Z', data: {}},
     {time: '2022-01-28T14:00:00Z', data: {}},     
     {time: '2022-01-28T15:00:00Z', data: {}},    
     {time: '2022-01-28T16:00:00Z', data: {}},    
     {time: '2022-01-29T18:00:00Z', data: {}},    
     {time: '2022-01-29T19:00:00Z', data: {}},    
     {time: '2022-01-30T08:00:00Z', data: {}},    
     {time: '2022-01-30T09:00:00Z', data: {}},    
     {time: '2022-01-30T10:00:00Z', data: {}},    
     {time: '2022-01-30T11:00:00Z', data: {}},    
    ];

   sortData = daysdata.sort((a,b) => (a.time < b.time) ? 1 : ((b.time < a.time) ? -1 : 0));

   console.log(sortData.slice(0,7));

一个算法是:

  1. 按时间值对数据进行排序属性
  2. 查找与开始日期相同的第一天的索引
  3. 如果找到一个元素,将该元素推入数组
  4. 将搜索日期增加 1 天

词法排序用于按 time 排序并查找 return 的记录,因为它是 ISO 8601 字符串。转换为 Date 对象也可以,但这似乎没有必要。

到 select 每个日期,dStart 设置为 UTC 日的开始,dEnd 设置为UTC 日结束。如果在该范围内找不到日期,则将 none 添加到数组中。这可以防止在 selected 数组中产生同一对象的多个条目(即间隔之后的第一个条目)的数据出现多天间隔的情况。

如果这应该基于本地日期,则使用 setHours 而不是 setUTCHours

/* Given data as an array of objects, return
 * first on date plus first on next n days
 * Dates are UTC, not local
 */
function getFirstNDays(data, n = 1, date = new Date()) {

  // Make sure n is a positive integer
  if (n % 1 || n < 0) return;

  data.sort((a, b) => a.time.localeCompare(b.time)); 

  let dStart = new Date(date);
  dStart.setUTCHours(0,0,0,0);

  let dEnd = new Date(dStart);
  dEnd.setUTCHours(23,59,59,999);
  let selected = [];
  
  while (n--) {
    let index = data.findIndex(obj => obj.time >= dStart.toISOString() && obj.time <= dEnd.toISOString());

    // If record found, add to selected array
    if (index != -1) {
      selected.push(data[index]);
    }

    dStart.setUTCDate(dStart.getUTCDate() + 1);
    dEnd.setUTCDate(dEnd.getUTCDate() + 1);
  }

  // Return selected values
  return selected;
}

let daysdata = [
  {time: '2022-01-20T11:00:00Z', data: {}},   
  {time: '2022-01-23T11:00:00Z', data: {}},   
  {time: '2022-01-23T12:00:00Z', data: {}},   
  {time: '2022-01-24T11:00:00Z', data: {}},   
  {time: '2022-01-24T12:00:00Z', data: {}},   
  {time: '2022-01-24T17:00:00Z', data: {}},   
  {time: '2022-01-24T23:00:00Z', data: {}},   
  {time: '2022-01-25T00:00:00Z', data: {}},   
  {time: '2022-01-25T04:00:00Z', data: {}},   
  {time: '2022-01-25T05:00:00Z', data: {}},   
  {time: '2022-01-25T06:00:00Z', data: {}},    
  {time: '2022-01-25T07:00:00Z', data: {}},   
  {time: '2022-01-25T14:00:00Z', data: {}},        
  {time: '2022-01-25T15:00:00Z', data: {}},    
  {time: '2022-01-26T13:00:00Z', data: {}},
  {time: '2022-01-26T14:00:00Z', data: {}},
  {time: '2022-01-26T15:00:00Z', data: {}},
  {time: '2022-01-27T13:00:00Z', data: {}},
  {time: '2022-01-27T14:00:00Z', data: {}},
  {time: '2022-01-27T15:00:00Z', data: {}},
  {time: '2022-01-28T14:00:00Z', data: {}},     
  {time: '2022-01-28T15:00:00Z', data: {}},    
  {time: '2022-01-28T16:00:00Z', data: {}},    
  {time: '2022-01-29T18:00:00Z', data: {}},    
  {time: '2022-01-29T19:00:00Z', data: {}},    
  {time: '2022-01-30T08:00:00Z', data: {}},    
  {time: '2022-01-30T09:00:00Z', data: {}},    
  {time: '2022-01-30T10:00:00Z', data: {}},    
  {time: '2022-01-30T11:00:00Z', data: {}},    
];

console.log(getFirstNDays(daysdata, 7, new Date(Date.UTC(2022,0,23))));

已更新以满足更新后的要求。