【问题标题】:Filter a range of dates from an array从数组中过滤一系列日期
【发布时间】:2021-01-24 07:14:37
【问题描述】:

我正在读取一个 .txt 文件,该文件已拆分为包含信息行的数组。

这是我原来的.txt

|datestamp              |endpoint    |id    |
|2019-03-01 09:00:00UTC |/co.html    |12345 |
|2019-03-01 09:00:00UTC |/co.html    |12346 |
|2019-03-01 10:00:00UTC |/co.html    |12345 |
|2019-03-01 10:30:00UTC |/hello.html |12347 |
|2019-03-01 11:00:00UTC |/co.html    |12347 |
|2019-03-02 11:00:00UTC |/co.html    |12348 |
|2019-03-02 12:00:00UTC |/hello.html |12348 |
|2019-03-03 13:00:00UTC |/hello.html |12349 |

所以现在我得到了这个信息数组:

[
'|datestamp              |endpoint    |id    |',
'|2019-03-01 09:00:00UTC |/co.html    |12345 |',
'|2019-03-01 09:00:00UTC |/co.html    |12346 |',
'|2019-03-01 10:00:00UTC |/co.html    |12345 |',
'|2019-03-01 10:30:00UTC |/hello.html |12347 |',
'|2019-03-01 11:00:00UTC |/co.html    |12347 |',
'|2019-03-02 11:00:00UTC |/co.html    |12348 |',
'|2019-03-02 12:00:00UTC |/hello.html |12348 |',
'|2019-03-03 13:00:00UTC |/hello.html |12349 |',
''
]

所以我需要过滤说这些日期戳 2019-03-01 10:00:00UTC - 2019-03-02 11:00:00UTC

我需要用松树“|”分割数组吗?在这样做之前呢?

我该如何解决这个问题?

【问题讨论】:

  • 是的,用|分割每一行。
  • 感谢@IslamElshobokshy 的回复,虽然我也是这样,但后来 ADT 展示了另一个版本。

标签: javascript arrays filter date-range


【解决方案1】:

请尝试一下。

let res = [
'|datestamp              |endpoint    |id    |',
'|2019-03-01 09:00:00UTC |/co.html    |12345 |',
'|2019-03-01 09:00:00UTC |/co.html    |12346 |',
'|2019-03-01 10:00:00UTC |/co.html    |12345 |',
'|2019-03-01 10:30:00UTC |/hello.html |12347 |',
'|2019-03-01 11:00:00UTC |/co.html    |12347 |',
'|2019-03-02 11:00:00UTC |/co.html    |12348 |',
'|2019-03-02 12:00:00UTC |/hello.html |12348 |',
'|2019-03-03 13:00:00UTC |/hello.html |12349 |',
''
]
let dates = [];
for(let i=1;i<res.length-1;i++)
{
  let splitedItem = res[i].split('|');
  dates.push(new Date(splitedItem[1].replace("UTC","").trim()));
}
console.log(dates)

现在你有了一个日期数组,你可以用 lambda 表达式查询这个数组。

【讨论】:

  • 感谢您的回复!我将研究 lambda 表达式,还没有听说过。
  • 虽然这将产生一个日期数组,但您现在已将其余字符串内容与日期解除关联。我假设过滤的目的还在于识别与这些日期相关的“端点”和“id”值。
【解决方案2】:

不,您不需要先拆分数组的元素。您可以使用部分字符串创建 Date() 对象,并将这些对象与代表您的开始和结束日期/时间的 Date() 对象进行比较:

let mydates = [
'|datestamp              |endpoint    |id    |',
'|2019-03-01 09:00:00UTC |/co.html    |12345 |',
'|2019-03-01 09:00:00UTC |/co.html    |12346 |',
'|2019-03-01 10:00:00UTC |/co.html    |12345 |',
'|2019-03-01 10:30:00UTC |/hello.html |12347 |',
'|2019-03-01 11:00:00UTC |/co.html    |12347 |',
'|2019-03-02 11:00:00UTC |/co.html    |12348 |',
'|2019-03-02 12:00:00UTC |/hello.html |12348 |',
'|2019-03-03 13:00:00UTC |/hello.html |12349 |',
''
]

let startDate = new Date("2019-03-01 10:00:00");
let endDate = new Date("2019-03-02 11:00:00");

let filtered = mydates.filter(d => new Date(d.substr(1, 19)) >= startDate && new Date(d.substr(1, 19)) <= endDate);
console.log(filtered);

【讨论】:

  • 感谢@ADT,它简洁易懂!
猜你喜欢
  • 2011-06-10
  • 2012-12-09
  • 1970-01-01
  • 1970-01-01
  • 2016-07-17
  • 2020-11-29
  • 2020-08-03
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多