Filter by date

In Ember, it’s easy to filter the array in which you are looking for the appropriate values ​​(Only return name == "John). I can’t figure out how to filter with a higher or lower value (return all objects whose startDate to this day

In my application, I have a result set. I want to divide these results into three categories: Debt for ten days, Overdue, and then the rest.

I found the following example in another SO post, but can't figure out how to use it to achieve my goal

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

You can simply do:

 this.get('content').filter(function(item){ if(item.get('someProperty') > someVar) { return true; } }); 
+4
source

This should return an array of objects within a specific date range. Should work in 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 can even do something like this:

 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. https://emberjs.com/api/classes/Ember.computed.html#method_filterBy

Also useful: https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Array/filter

0
source

All Articles