簡體   English   中英

按日期范圍過濾

[英]Filter by date range

在Ember中,很容易過濾一個數組,你正在尋找匹配的值(只返回名稱==“約翰”)我不知道如何過濾大於或小於(返回其startDate為的所有對象)在今天之前

在我的應用程序中,我有一系列可交付成果。 我想將這些可交付成果分為三類:十天內到期,過期,然后是其他。

我在另一篇SO帖子中找到了以下示例,但無法弄清楚如何使用它來實現我的目標

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

你可以這樣做:

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

這應該返回您定義的日期范圍內的對象數組。 應該在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'); }); }) 

使用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')); }) 

如果您有更簡單的要求, computed.filterBy()可能適合您。 https://emberjs.com/api/classes/Ember.computed.html#method_filterBy

也很有幫助: 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