简体   繁体   English

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

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

I have an array of ISO dates I'm trying to sort and get only the next seven days from it我有一系列 ISO 日期,我正在尝试对其进行排序并从中获取接下来的 7 天

Here is the sample这是示例

 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: {…}}, ]

That is the sample code.那是示例代码。

just sort by Array.sort and pick the first n elements of an array by Array.slice只需按 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));

An algorithm is:一个算法是:

  1. Sort the data by the value of the time property按时间属性的值对数据进行排序
  2. Find the index of the first day with a time that is equal to or later than the supplied date (default is the current date)查找时间等于或晚于提供的日期的第一天的索引(默认为当前日期)
  3. Find the index of the first day with a time that is equal to or later than the current date + n days查找时间等于或晚于当前日期 + n 天的第一天的索引
  4. Get all entries from the first index to the second index minus 1获取从第一个索引到第二个索引的所有条目减 1

A lexical sort is used to sort on time and to find the records to return as it's an ISO 8601 string.词法排序用于按时间排序并查找要返回的记录,因为它是 ISO 8601 字符串。 Conversion to Date objects will work too but that seems unnecessary.转换为 Date 对象也可以,但这似乎没有必要。

For the compare strings the local time is set to the start of the day using setHours .对于比较字符串,本地时间使用setHours设置为一天的开始。 If that doesn't suit, change it.如果这不适合,请更改它。

Eg例如

 function getNextNDays(data, n, date = new Date()) { data.sort((a, b) => a.time.localeCompare(b.time)); let d = new Date(date); d.setHours(0,0,0,0); let startDate = d.toISOString(); d.setDate(d.getDate() + Number(n)); let endDate = d.toISOString(); let startIndex = data.findIndex(obj => obj.time >= startDate); let endIndex = data.findIndex(obj => obj.time >= endDate); // If no suitable record found for start, return if (startIndex == -1) return []; if (endIndex == -1) endIndex = data.length; // Debug console.log(`Extracting data for ${endIndex - startIndex} records from: ` + `\n${startDate} to \n${endDate}`); // Return array of selected values return data.slice(startIndex, endIndex); } 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(getNextNDays(daysdata, 7, new Date(2022,0,23)));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM