Angularfire2 - 根据其中一个值对对象进行排序?

Angularfire2 - Sorting objects based on one of it's values?

我正在尝试根据每个对象的日期值对每个对象进行排序并获取每个唯一的日期。

假设我们在 Firebase 的列表下有很多这样的对象:

{
    object123: {
        date: "11-12-2016", 
        title: "These nuts"
    }
}

在部分视图中,我有这样的内容:

<div *ngFor="let date of uniqueDates">
  <h1>{{date}}</h1>
</div>

为了获得每个唯一的日期,我制作了一个数组 uniqueDates,它应该包含每个唯一的日期。

为了实现这一点,我尝试这样做:

 /* This was supposed to add each date value from all the objects to uniqueDates, 
 ** but did not work.
 ** "items" is a FirebaseListObservable
 */
 this.items.forEach(
   item => {item.subscribe(item => this.uniqueDates.push(item.date))}
 );

 /*This makes each value unique*/
 this.uniqueDates= Array.from(new Set(uniqueDates));

要获取与日期对应的每个对象,我继续这样做:

<div *ngFor="let date of uniqueDates">
  <h1>{{date}}</h1>

  <div *ngFor="let item of items | async"> //Items is a FirebaseListObservable containing all the objects
    <div *ngIf="item.date is equal to date">
      <p>{{item.someData}}<p>
    <div>
  </div>
</div>

我应该采用这种方法吗?如果是这样,我如何以正确的方式将每个唯一日期推送到数组?你会如何处理这个问题?

你可以试试这个:

let flags:any = [];
let arrLength = this.items.length;
this.uniqueDates = [];

for(let count=0; i<arrLength; count++) {
  if(flags[this.items[count].date]) continue;
  flags[this.items[count].date] = true;
  this.uniqueDates.push(this.items[count]);
}

现在绑定uniqueDates数组进行查看。希望这对你有用。