简体   繁体   English

按日期范围过滤

[英]Filter by date range

In Ember it is easy to filter an array where you are looking for matching values ( Only return name == "John) What I can't figure out is how to filter with a greater than or less than ( Return all objects whose startDate is before today 在Ember中,很容易过滤一个数组,你正在寻找匹配的值(只返回名称==“约翰”)我不知道如何过滤大于或小于(返回其startDate为的所有对象)在今天之前

In my app I have a collection of deliverables. 在我的应用程序中,我有一系列可交付成果。 I want to divide these deliverables into three categories: Due within ten days, Past due, and then the rest. 我想将这些可交付成果分为三类:十天内到期,过期,然后是其他。

I found the following example in another SO post, but can't figure out how to use it to accomplish my goal 我在另一篇SO帖子中找到了以下示例,但无法弄清楚如何使用它来实现我的目标

filterComputed: function() {
  return this.get('content').filter(function(item, index, enumerable){
    return item.firstName == 'Luke';
  });
}.property('content.@each')

You can just do: 你可以这样做:

this.get('content').filter(function(item){
    return item.get('someProperty') > someVar;
});

This should return an array of objects within your defined date range. 这应该返回您定义的日期范围内的对象数组。 Should work in Ember ^2.x. 应该在Ember ^ 2.x中工作。

 filterComputed: computed('content.@each', 'startDate', 'endDate', function() { return this.get('content').filter(function(item) { var contentDate = item.get('date'); // expecting item to have a date property return contentDate > this.get('startDate') && bookingDate < this.get('endDate'); }); }) 

With ES6 you could even do something like this: 使用ES6,您甚至可以执行以下操作:

 filterComputed: computed('content.@each', 'startDate', 'endDate', function() { return this.get('content').filter(item => item.get('date') > this.get('startDate') && item.get('date') < this.get('endDate')); }) 

If you have a simpler requirement, computed.filterBy() might be right for you. 如果您有更简单的要求, computed.filterBy()可能适合您。 https://emberjs.com/api/classes/Ember.computed.html#method_filterBy https://emberjs.com/api/classes/Ember.computed.html#method_filterBy

Also helpful: https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Array/filter 也很有帮助: https//developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Array/filter

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM