2017-09-22 150 views
0

我有一个json数据如下。我正在使用angular2打字稿来根据日期范围筛选此json。我正在使用Moment格式化日期字符串。我需要针对类的所有属性过滤searchvalue。我与其他物业管理。我不知道如何过滤日期范围。任何人都可以帮助如何做到这一点?typescript如何按日期范围过滤json

过滤标准: 导入*为'瞬间'的时刻;

export class FilterCriteria { 
    public from: string; 
    public to: string; 
    public searchValue: string; 
    constructor() { 
     this.from = moment(new Date()).format('DD/MM/YYYY').toString(); 
     this.to = moment(new Date()).format('DD/MM/YYYY').toString(); 
    } 
} 

搜索方法:

return this.searchItems 
      .map((item) => 
       items.filter(item => item.id === criteria.searchValue 
          || item.time === criteria.searchValue 
          || item.alertType.toLocaleLowerCase().indexOf(criteria.searchValue.toLocaleLowerCase()) > -1 
          || item.source === criteria.searchValue 
          || item.relevantComms.toLocaleLowerCase().indexOf(criteria.searchValue.toLocaleLowerCase()) > -1 
          || item.to.toLocaleLowerCase().indexOf(criteria.searchValue.toLocaleLowerCase()) > -1 
          || item.from.toLocaleLowerCase().indexOf(criteria.searchValue.toLocaleLowerCase()) > -1)); 

和JSON数据:

[ 
    { 
     "from": "Sender", 
     "to": "Receiver", 
     "source": "Email", 
     "id": "861068CM", 
     "date": "24/07/17" 
    }, 
    { 
     "from": "Sender", 
     "to": "Receiver", 
     "source": "Email", 
     "id": "861068CM", 
     "date": "12/07/17" 
    }, 
    { 
     "from": "Sender", 
     "to": "Receiver", 
     "source": "Email", 
     "id": "861068CM", 
     "date": "09/07/17" 
    }, 
    { 
     "from": "Sender", 
     "to": "Receiver", 
     "source": "Email", 
     "id": "861068CM", 
     "date": "02/02/17" 
    }, 
] 
+0

是什么类型,并从性质的项目?他们是日期还是时刻对象? – hagner

+0

from和to是字符串属性。该过滤器必须应用于json中的date属性 –

回答

0

我会用瞬间query methods isBefore和isAfter方法比较的日期。 在FilterCriteria类从和属性的类型更改为时刻类型:

export class FilterCriteria { 
    public from: Moment; 
    public to: Moment; 
    public searchValue: string; 
    constructor() { 
     this.from = moment(new Date()); 
     this.to = moment(new Date()); 
    } 
} 

在过滤器中,然后使用它们:

criteria.from.isBefore(item.date) && criteria.to.isAfter(item.date)