基于日期属性的fiter对象数组

时间:2018-04-17 12:01:18

标签: javascript arrays date ionic-framework ionic2

我想根据当前月份显示对象 我的代码是

 for (let m of  this.monthEvent){
     m=new Date();            
         if(m.getMonth() === new Date().getMonth()){
           this.thisMonth=m;
   }
     return this.thisMonth;

我的数组是

[{title: "", date: "2018-03-29"},
 {title: "", date: "2018-04-13"},
 {title: "", date: "2018-04-12"},
 {title: "leave", date: "2018-04-11"}
 {title: "", date: "2018-04-16"}]

它显示m.getTime不是函数

我想显示当前月份事件

提前致谢

1 个答案:

答案 0 :(得分:1)

  • 您可以使用函数filter来获取特定事件。
  • 使用此解构分配var [_, month] = e.date.split('-');
  • 获取活动的月份

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-04-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);
.as-console-wrapper { max-height: 100% !important; top: 0; }