在 JS 中过滤数组以持续 12 个月

Filter array to last 12 months in JS

给定一个数组对象,将数组过滤为 return 最近 12 个月的数据。

示例 1:今天是 2021 年 5 月,因此 returned 数据日期范围是 2020 年 6 月到 2021 年 5 月。

示例 2:对于 2016 年,returned 数据日期范围在 2015 年 6 月到 2016 年 5 月之间。

var observations = [
 {gameScore: "1", reportDate: "2021-09-09"},
 {gameScore: "1", reportDate: "2021-05-09"},
 {gameScore: "1", reportDate: "2020-06-09"},
 {gameScore: "1", reportDate: "2020-04-09"},
 {gameScore: "1", reportDate: "2019-08-09"},
 {gameScore: "1", reportDate: "2019-03-09"},
];

//example 1
const example1Result = _.filter(observations, o=> moment()
  .add(-12, "months")
  .isSameOrBefore(moment(o.reportDate)));
console.log(example1Result);

//example 2
const example2Result = _.filter(observations, o=> !moment()
  .add(-1, "year")
  .isAfter(moment(o.reportDate)));
console.log(example2Result);
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.29.1/moment.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.21/lodash.min.js"></script>

但是,它 return 是 2021 年 6 月至 12 月,在这种情况下,它不应该。不确定如何实现示例 2?

Fiddle: https://jsfiddle.net/y912drhm/

除了月份的 -12 之外,您还必须为年份做一个 -1,以便参考上一年。

您可以使用 isBetween moment 方法(并使用 .subtract() 替代 .add(-12 ..)

let result = observations.filter(o => moment(o.reportDate, 'YYYY-MM-DD').isBetween(moment().subtract(12, 'months'), moment(), undefined, '[]'));

像第四个参数的[只是为了包括([])或排除(())用于日期范围限制。 [这里是文档][1]

var observations = [
  {gameScore: "1", reportDate: "2021-09-09"},
  {gameScore: "1", reportDate: "2021-05-09"},
  {gameScore: "1", reportDate: "2020-06-09"},
  {gameScore: "1", reportDate: "2020-04-09"},
  {gameScore: "1", reportDate: "2019-08-09"},
  {gameScore: "1", reportDate: "2019-03-09"},
];

result = observations.filter(o => moment(o.reportDate, 'YYYY-MM-DD').isBetween(moment().subtract(12, 'months'), moment(), '[]'));

console.log(result);
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.29.1/moment.min.js"></script>

[1]: https://momentjs.com/docs/#/query/is-between/

示例 2

var example1 = [
{gameScore: "1", reportDate: "2021-09-09"},
{gameScore: "1", reportDate: "2021-05-09"},
{gameScore: "1", reportDate: "2021-04-09"},
{gameScore: "1", reportDate: "2020-08-09"},
{gameScore: "1", reportDate: "2020-06-09"},
{gameScore: "1", reportDate: "2020-04-09"},
{gameScore: "1", reportDate: "2019-08-09"},
{gameScore: "1", reportDate: "2019-03-09"},
];

var example2 = [
{gameScore: "1", reportDate: "2016-09-09"},
{gameScore: "1", reportDate: "2016-05-09"},
{gameScore: "1", reportDate: "2016-04-09"},
{gameScore: "1", reportDate: "2015-08-09"},
{gameScore: "1", reportDate: "2015-06-09"},
{gameScore: "1", reportDate: "2015-04-09"},
{gameScore: "1", reportDate: "2014-08-09"},
{gameScore: "1", reportDate: "2014-03-09"},
];

let referenceYear = 2016

let result = example1.filter(o => moment(o.reportDate, 'YYYY-MM-DD').isBetween(moment().subtract(12, 'months'), moment(), '[]'));
console.log(result);

let result2 = example2.filter(o => moment(o.reportDate, 'YYYY-MM-DD').isBetween(moment(new Date(new Date().setFullYear(referenceYear-1))), moment(new Date(new Date().setFullYear(referenceYear))), '[]'));
console.log(result2);
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.29.1/moment.min.js"></script>