2016-11-13 89 views
0

我想修改然後對列表進行排序。我完全小白在這個:(如何使用Observable參數對Observable進行排序? rxjs

我能夠與「combineLatest」做,但問題是,我始終以修改陣列下面「groupByDateAndTournament」功能 我的結果:

  1. 創建觀察的名單和排序參數,僅在觀察的名單已經改變喜愛
  2. 排序(favoriteTournaments,teamRanks)
  3. 修改清單與「groupByAndTournament」只有obsevable favoriteTournament改變
  4. 排序行列只有當觀察到teamRank改變

    this.subscription = 
        this.matchesService.getUpcoming() 
        .merge(
        this.favoriteService.getFavoriteTournaments().flatMap((data) => { 
         return {'favoriteTournaments': data} 
        }), 
        this.teamsService.getTeamRanking().flatMap((data) => { 
         return {'teamRanks': data} 
        }) 
    
    ).scan((acc, curr) => { 
        let upcomingMatches; 
        if (curr.upcoming) { 
         upcomingMatches = this.groupByDateAndTournament(curr); 
        } 
        if (curr.favoriteTournaments) { 
         upcomingMatches = this.sortByFavorite(curr) 
        } 
    
        if (curr.teamRanks) { 
         upcomingMatches = this.sortByRank(curr); 
        } 
        return upcomingMatches; 
    
    }) 
        .subscribe() 
    
+0

爲什麼不使用普通的'Array.sort(...)'?很難理解這段代碼應該做什麼... – martin

回答

0

所以我combineLatest

管理它
Observable.combineLatest(
     this.matchesService.getUpcoming().map((data)=> this.upcomingService.combineUpcomings(data)), 
     this.favoriteService.getFavoriteTournaments(), 
     this.teamsService.getTeamRanking(), 
     (matches, favoriteTournaments, teamRanks) => this.upcomingService.prepareUpcomingMatches(matches, favoriteTournaments, teamRanks) 
    ).subscribe((data)=> 
     this.ngZone.run(() => { 
      this.upcomingMatches = data; 
      this.loading = false; 
     }) 
    ); 

它仍然有問題。 PrepareUpcomingMatches當任何觀測值發生變化時,函數將重新運行。

相關問題