【问题标题】:filter array of objects based on month and year根据月份和年份过滤对象数组
【发布时间】:2018-11-23 21:07:27
【问题描述】:

我想根据当前月份和年份显示对象

这会根据月份过滤对象,我还需要根据年份过滤对象。

var array = [{
    title: "a",
    date: "2018-03-29"
  }, {
    title: "b",
    date: "2018-04-13"
  }, {
    title: "c",
    date: "2018-04-12"
  }, {
    title: "leave",
    date: "2018-04-11"
  }, {
    title: "d",
    date: "2018-06-16"
  }],
  currentMonth = new Date().getMonth() + 1,
  events = array.filter(e => {
    var [_, month] = e.date.split('-'); // Or, var month = e.date.split('-')[1];
    return currentMonth === +month;
  });
console.log(events);

【问题讨论】:

  • 使用year而不是_并对照当年检查?
  • @CertainPerformance 谢谢我会
  • var [year, month] = e.date.split('-'); return currentYear === +year && currentMonth === +month;

标签: javascript arrays typescript object


【解决方案1】:

要过滤yearmonth,你只需要得到currentYearcurrentMonth,然后得到yearmonth的迭代month

这应该是你的代码:

//Get the currentYear and the currentMonth
currentMonth = new Date().getMonth() + 1,
currentYear = new Date().getFullYear(),

//Get the year and month from the iterated date
var [year, month] = e.date.split('-');

//Then filter the dates
events = array.filter(e => {
    var [year, month] = e.date.split('-'); // Or, var month = e.date.split('-')[1];
    return (currentMonth === +month) && (currentYear == year);
});

演示:

var array = [{
    title: "a",
    date: "2018-03-29"
  }, {
    title: "b",
    date: "2018-04-13"
  }, {
    title: "c",
    date: "2018-04-12"
  }, {
    title: "leave",
    date: "2018-04-11"
  }, {
    title: "d",
    date: "2018-06-16"
  }],
  currentMonth = new Date().getMonth() + 1,
  currentYear = new Date().getFullYear(),
  events = array.filter(e => {
    var [year, month] = e.date.split('-'); // Or, var month = e.date.split('-')[1];
    return (currentMonth === +month) && (currentYear == year);
  });
console.log(events);

【讨论】:

  • 这已经在 20 分钟前的第一条评论中得到了回答。在过滤器之前也不需要var [year, month] = e.date.split('-');
  • @mplungjan 是的,它在这条评论中得到了部分回答(我在做出回答之前已经投票赞成),在这里使用破坏非常简单;)是的,我刚刚做了一个完整的答案,包括得到年份和过滤。 :)
【解决方案2】:

您可以像2018-06一样创建一个表示年份和月份的字符串,并在date属性中将该值作为子字符串检查,以过滤掉当年和当前月份的记录。

var array = [{
    title: "a",
    date: "2018-03-29"
  }, {
    title: "b",
    date: "2018-04-13"
  }, {
    title: "c",
    date: "2018-04-12"
  }, {
    title: "leave",
    date: "2018-06-11"
  }, {
    title: "d",
    date: "2018-04-16"
  },
   {
    title: "e",
    date: "2018-06-18"
  }],
  currentMonth = '0'+(new Date().getMonth() + 1),
  currentYear = new Date().getFullYear()
  events = array.filter((e) => {
    var dateStr = currentYear+'-'+currentMonth;
    return (e.date.indexOf(dateStr) !== -1)
  });
console.log(events);

【讨论】:

  • 为什么不只是var [year, month] = e.date.split('-'); return currentYear === +year && currentMonth === +month;
  • @mplungjan 也可以使用解构来完成,但这是我向 OP 展示的其他替代方法。
  • 保持简单。在破坏中添加年份要简单得多
  • @mplungjan 是的。但是当其他用户访问这个或 OP 本身时,这对于开发逻辑也很有用。
  • 我有一种强烈的感觉,你的解决方案比包括 int 强制转换在内的数字比较重
猜你喜欢
  • 2021-11-25
  • 1970-01-01
  • 2017-07-01
  • 1970-01-01
  • 2021-12-07
  • 2021-05-18
  • 1970-01-01
  • 2020-11-21
  • 1970-01-01
相关资源
最近更新 更多