过滤响应数据日期明智vue

时间:2018-04-05 06:58:27

标签: date filter vue.js

我们如何在vue2中过滤响应数据? (本月数据,今年数据,本周数据)当我点击“月份”时按钮我希望仅获得本月数据。

我收到了这样的回复 - [{" date":" 2017-02-05"," views":1," readMore& #34;:1},{" date":" 2018-03-15"," views":1," readMore": 1},{" date":" 2018-01-27"," views":2," readMore":2}]。

提前致谢

computed: {
  DateWise: function () {
    if(this.duration === 'year'){ 
      return this.data.filter(item => item.date.includes(new Date().getFullYear()));
    }
    else if(this.duration === 'month'){ 
      return this.data.filter(item => item.date.includes(new Date().getMonth()+1));
    }
  }
},

1 个答案:

答案 0 :(得分:0)

JSON并不了解日期,因此一个好的做法是在反序列化时将日期字符串转换为日期对象。然后,您希望将源数据放在存储中,并为已过滤的数组创建计算属性。

Here is a working example.

这里是过滤属性的代码......

displayDates(){
    var now = new Date(Date.now());
    switch(vueStore.currView){
    case 'all': return vueStore.allDates;
    case 'month' : return vueStore.allDates.filter(
        line => line.date > new Date(now.getFullYear(), now.getMonth(),0)
    );
    case 'year' : return vueStore.allDates.filter(
        line => line.date > new Date(now.getFullYear(), 0,0)
    )
  }
}